尝试计算一年中的周数,下面是代码,但它返回了错误的周数。
{{1}}
2015 = 53周,但现在又回来了52周。
答案 0 :(得分:0)
- (NSInteger) getNumberOfWeeks : (NSDate *) fromDate {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSCalendarUnitWeekOfYear fromDate:fromDate];
return [components weekOfYear];
}
或试试这个
NSDate *date = [NSDate date];//since you are forming date, put it here
NSCalendar *calender = [NSCalendar currentCalendar];
NSRange weekRange = [calender rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
NSInteger weeksCount=weekRange.length;
NSLog(@"%d",weeksCount);
此方法将返回从12月31日到您在此函数中传递的日期的星期数。