当系统日历在星期天,NSCalendar认为它是新的一周

时间:2014-09-22 20:47:39

标签: ios object nsdate nscalendar

当我的系统日历在星期日设置时,下面的代码认为它是新的一周,并在下周显示所有日期。 例如 本周是第39周。 在9月28日星期日,我的代码认为这是一个新的一周,并显示下周(第40周)的所有日子,而不是我想要的第39周。 (我住的地方一周,从周一到周日,而不是周日到周六)

NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [NSCalendar currentCalendar];

[myCalendar setLocale:[NSLocale systemLocale]];

    NSDateComponents *currentComps = [myCalendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:currentDate];
int thisWeeksNumber = currentComps.weekOfYear;
NSLog(@"1  %d", thisWeeksNumber);

[myCalendar setFirstWeekday:2];


[currentComps setWeekday:2];
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:3];
NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:4];
NSDate *thirdDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:5];
NSDate *fourthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:6];
NSDate *fifthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7];
NSDate *sixthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:1];
NSDate *seventhDayOfTheWeek = [myCalendar dateFromComponents:currentComps];


NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

我的日志显示: 2014-09-21 22:40:36.040 NSCalendarProject [38667:90b]  2014年9月22日  2014年9月23日  2014年9月24日  2014年9月25日  2014年9月26日  2014-09-27  2014年9月28日

正如你所看到的那样,我在9月21日的星期日运行它的日期显示了下周的所有日子(如果我在9月28日设定日期,那就相同了)< / p>

1 个答案:

答案 0 :(得分:2)

调用[myCalendar setFirstWeekday:2]仅设置星期一的第一个工作日。但是,周日仍然是'1'。所以,当currentDate是星期日时,'2'指的是星期日之后的星期一,你的代码最后会显示下周的日期。

你真正想要的是获得'currentDate'所属的一周的第一天。这段代码应该可行,但我不知道它是否是最优雅的解决方案。

NSCalendar *myCalendar = [NSCalendar currentCalendar];

    //create a 'sunday' date object for demonstration purposes
    NSDateComponents *sundayComponents = [[NSDateComponents alloc]init];
    sundayComponents.day = 28;
    sundayComponents.month = 9;
    sundayComponents.year = 2014;
    NSDate *sunday = [myCalendar dateFromComponents:sundayComponents];

    [myCalendar setLocale:[NSLocale systemLocale]];

    NSDateComponents *currentComps = [myCalendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSCalendarUnitWeekOfYear|NSWeekdayCalendarUnit fromDate:sunday];
    int thisWeeksNumber = currentComps.weekOfYear;
    NSLog(@"1  %d", thisWeeksNumber);

    [myCalendar setFirstWeekday:2];
    NSDate *beginningOfWeek = nil;
    [myCalendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&beginningOfWeek
                   interval:NULL forDate: sunday];


    NSDateComponents *components = [[NSDateComponents alloc] init];

    NSDate *firstDayOfTheWeek = beginningOfWeek;
    components.day = 1;
    NSDate *secondDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 2;
    NSDate *thirdDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 3;
    NSDate *fourthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 4;
    NSDate *fifthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 5;
    NSDate *sixthDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];
    components.day = 6;
    NSDate *seventhDayOfTheWeek = [myCalendar dateByAddingComponents:components toDate:beginningOfWeek options:0];


    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    myDateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
    NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
    NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
    NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
    NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
    NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
    NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

    NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

    //log first day of week
    NSString *beginningWeek = [myDateFormatter stringFromDate:beginningOfWeek];
    NSLog(@"beginning of week is %@", beginningWeek);