获取当前系统时间计算后面的七天的日期和星期,本周,本月

在计算时间之时,需要判断是否是闰年还是平年,闰年2月29天,平年2月28天:

获取当前时间

-(NSString *)obtainSystemTimeAndDate{

    NSDate *  senddate=[NSDate date];
    NSCalendar  * cal=[NSCalendar  currentCalendar];
    NSUInteger  unitFlags=NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year=[conponent year];
    NSInteger month=[conponent month];
    NSInteger day=[conponent day];
    NSString  *nsDateString= [NSString  stringWithFormat:@"%4ld年%2ld月%2ld日",(long)year,(long)month,(long)day];
    NSLog(@"系统时间为%@",nsDateString);
    return nsDateString;

}

计算时间放到字典中去

-(NSString*)weekdayStringFromDate:(NSDate*)inputDate {

    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];

    [calendar setTimeZone: timeZone];

    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;

    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];

    return [weekdays objectAtIndex:theComponents.weekday];

}

获取本周的开始时间和结束时间
OC 版:

    NSString* string = @"2016-08-01";
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    NSLog(@"date = %@", inputDate);

//    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSDayCalendarUnit
                                         fromDate:inputDate];
    // 得到星期几
    // 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
    NSInteger weekDay = [comp weekday];
    // 得到几号
    NSInteger day = [comp day];

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

    // 计算当前日期和这周的星期一和星期天差的天数
    long firstDiff,lastDiff;
    if (weekDay == 1) {
        firstDiff = 1;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay;
        lastDiff = 9 - weekDay;
    }

    NSLog(@"firstDiff:%ld   lastDiff:%ld",firstDiff,lastDiff);

    // 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:inputDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];

    NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:inputDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"星期一开始 %@",[formater stringFromDate:firstDayOfWeek]);
    NSLog(@"当前 %@",[formater stringFromDate:inputDate]);
    NSLog(@"星期天结束 %@",[formater stringFromDate:lastDayOfWeek]);

Swift版本:

let Now = NSDate();
        let calendar = NSCalendar.currentCalendar();
        let comp = calendar.components([NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day,NSCalendarUnit.Weekday], fromDate: Now);
        // 得到星期几
        // 1(星期天) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
        let weebDay = comp.weekday;
        // 得到几号
        let day = comp.day;
        // 计算当前日期和这周的星期一和星期天差的天数
        var firstDiff:NSInteger;
        var lastDiff:NSInteger;
        if weebDay == 1 {
            firstDiff = 1;
            lastDiff = 0;
        }else{
            firstDiff = calendar.firstWeekday - weebDay;
            lastDiff = 9-weebDay;
        }
        // 在当前日期(去掉了时分秒)基础上加上差的天数
        let firstDayComp = calendar.components([NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day,NSCalendarUnit.Weekday], fromDate: Now);
        firstDayComp.day = day + firstDiff;
        let firstDayOfWeek = calendar.dateFromComponents(firstDayComp);

        let lastDayComp = calendar.components([NSCalendarUnit.Year,NSCalendarUnit.Month,NSCalendarUnit.Day,NSCalendarUnit.Weekday], fromDate: Now);
        lastDayComp.day = day + lastDiff;
        let lastDayOfWeek = calendar.dateFromComponents(lastDayComp);
        let formater = NSDateFormatter();

        formater.dateFromString("yyyy-MM-dd HH:mm:ss");

        BeginTime = formater.stringFromDate(firstDayOfWeek!);
        EndTime = formater.stringFromDate(Now);

        print("星期一开始\(formater.stringFromDate(firstDayOfWeek!))");
        print("当前 \(formater.stringFromDate(Now!))");
        print("星期天结束\(formater.stringFromDate(lastDayOfWeek!))");

后面还有计算星期的时间,在之后加上。

你可能感兴趣的:(IOS基础实例)