当调用模拟器
版本 6.1 的方法时,它显示:
year---2012
week no---52
weekday---2
resultdate---2012-12-16 18:30:00 +0000
日期时间是 2012 年 12 月 17 日到 23 2012 年 12 月
版本 5.0 它显示:
年---2012 年
第 52 个
工作日---2结果日期
---2012-12-23 18:30:00 +0000
日期时间是 12 月 24 日。2012 年至 12 月 30 日 2012
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.Locale = [NSLocale currentLocale];
//[gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7
NSDateComponents *components = [gregorian components:NSYearForWeekOfYearCalendarUnit |NSWeekOfYearCalendarUnit |NSWeekdayOrdinalCalendarUnit fromDate:[NSDate date]];
[components setYearForWeekOfYear:2012];
[components setWeekOfYear:52];
[components setWeekdayOrdinal:2];
NSLog(@"year---%ld",(long)[components yearForWeekOfYear]);
NSLog(@"week no---%ld",(long)[components weekOfYear]);
NSLog(@"weekday---%ld",(long)[components weekdayOrdinal]);
NSDate *resultDate = [gregorian dateFromComponents:components];
NSLog(@"resultdate---%@",resultDate);
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[gregorian rangeOfUnit:NSWeekOfYearCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:resultDate];
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
并得到使用的输出日期格式化程序是
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *str_StartOfTheWeek = [dateFormatter stringFromDate:startOfTheWeek];
NSString *str_EndOfWeek = [dateFormatter stringFromDate:endOfWeek];
NSLog(@"date time is %@",[NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek]);
return [NSString stringWithFormat:@"%@ To %@",str_StartOfTheWeek,str_EndOfWeek];
如何解决这个问题请帮忙。