ios 時間一系列的問題

 

獲取今天字符串算法

 

+(NSString *)getCurrentDay{數組

 

    NSDate *currentDate = [NSDate date];spa

 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];component

 

    [dateFormatter setDateFormat:@"YYYY-MM-dd"];orm

 

    NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];字符串

 

    return currentDateStr;get

 

}string

 

 

獲取昨天字符串it

 

+(NSString *)getYesdayDay{io

 

    NSDate *currentDate = [NSDate date];

 

    currentDate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:currentDate];//前一天

 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

 

    [dateFormatter setDateFormat:@"YYYY-MM-dd"];

 

    NSString *currentDateStr = [dateFormatter stringFromDate:currentDate];

 

    return currentDateStr;

 

}

 

時間戳轉NSDate

-(NSDate *)nsstringConversionNSDate:(NSString *)dateStr

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSDate *datestr = [dateFormatter dateFromString:dateStr];

    return datestr;

}

 

NSDate轉字符串日期

- (NSString *)dateConversionTimeStamp:(NSDate *)date{

    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];

    return timeSp;

}

 

時間戳轉字符串日期

-(NSString *)timeStampConversionNSString:(NSString *)timeStamp{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateStr = [formatter stringFromDate:date];

    return dateStr;

}

四,NSDate日期 獲取 星期幾

- (NSString *) getweekDayStringWithDate:(NSDate *) date{

    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日曆的算法

    NSDateComponents *comps = [calendar components:NSWeekdayCalendarUnit fromDate:date];// 1 是週日,2是週一 3.以此類推

    NSNumber * weekNumber = @([comps weekday]);NSInteger weekInt = [weekNumber integerValue];

    NSString *weekDayString = @"週一";

    switch (weekInt) {

        case 1:{weekDayString = @"週日";}

        break;

        case 2: {

            weekDayString = @"週一";}

        break;

        case 3:{

            weekDayString = @"週二";

            

        }

        break;

        case 4:{

            weekDayString = @"週三";

        }

        break;

        case 5:{

            weekDayString = @"週四";

            

        }

        break;case 6:{

            weekDayString = @"週五";

        }

            break;

        case 7:{

            weekDayString = @"週六";

            

        }

        break;

        default:

        break;}

    return weekDayString; 

}

 

 當前時間所在一週第一天

+ (NSString *)getCurrentWeekFirstDay{

    NSDate *now = [NSDate date];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday

                                         fromDate:now];

    // 獲得星期幾

    // 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)

    NSInteger weekDay = [comp weekday];

    // 獲得幾號

    NSInteger day = [comp day];

    

    NSLog(@"weekDay:%ld  day:%ld",weekDay,day);

    

    // 計算當前日期和這周的星期一和星期天差的天數

    long firstDiff,lastDiff;

    if (weekDay == 1) {

        firstDiff = 1;

        lastDiff = 0;

    }else{

        firstDiff = ([calendar firstWeekday] - weekDay)+1;

        lastDiff = 7 - weekDay;

    }

    NSArray *currentWeeks = [self getCurrentWeeksWithFirstDiff:firstDiff lastDiff:lastDiff];

    // 在當前日期(去掉了時分秒)基礎上加上差的天數

    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

    [firstDayComp setDay:day + firstDiff];

    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];

    

    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];

    [lastDayComp setDay:day + lastDiff];

    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];

    

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];

    [formater setDateFormat:@"yyyy-MM-dd"];

    NSString *currentDateStr = [formater stringFromDate:firstDayOfWeek];

    return currentDateStr;

}

 

獲取一週時間 數組

+ (NSMutableArray *)getCurrentWeeksWithFirstDiff:(NSInteger)first lastDiff:(NSInteger)last{

    NSMutableArray *eightArr = [[NSMutableArray alloc] init];

    for (NSInteger i = first; i < last + 1; i ++) {

        //從如今開始的24小時

        NSTimeInterval secondsPerDay = i * 24*60*60;

        NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];         [dateFormatter setDateFormat:@"M月d日"];

        NSString *dateStr = [dateFormatter stringFromDate:curDate];//幾月幾號

        //        NSString *dateStr = @"5月31日";

        NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init];

        [weekFormatter setDateFormat:@"EEEE"];//星期幾 @"HH:mm 'on' EEEE MMMM d"];

        NSString *weekStr = [weekFormatter stringFromDate:curDate];

        //組合時間

        NSString *strTime = [NSString stringWithFormat:@"%@(%@)",dateStr,weekStr];          [eightArr addObject:strTime];

    }

    return eightArr;

}

相關文章
相關標籤/搜索