NSCalendar NSDateComponents weekofYear return 1 with date 2014-12-31

后端 未结 3 550
难免孤独
难免孤独 2021-01-28 17:01

I want to get weekofYear with date \'2014-12-31\' ,but it always return 1 not 52 here is my code:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSUIntege         


        
3条回答
  •  [愿得一人]
    2021-01-28 17:44

    Set the minimumDaysInFirstWeek property of the NSCalendar instance before obtaining the date components.

    NSCalendar *calendar = [NSCalendar currentCalendar];
    calendar.minimumDaysInFirstWeek = 7; // the week has to be completely within one year
    
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:[NSDate date]];
    
    NSLog(@"%i",dateComponent.weekOfYear); // 52
    

提交回复
热议问题