如何获得给定年份的周数

时间:2015-09-03 18:08:19

标签: ios calendar

我已经成功检索了一个星期的数字,但是现在我想知道给定年份中有多少个星期。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用NSCalendarNSDateComponents

获取周数
- (NSInteger) getNumberOfWeeks : (NSDate *) fromDate {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:NSCalendarUnitWeekOfYear fromDate:fromDate];
    return [components weekOfYear];
}

此方法将返回从12月31日到您在此函数中传递的日期的星期数。