如何获得给定年份的周数

How to get the number of weeks from given year

我已经成功地从日期中检索到周数,但现在我想获取给定年份中有多少周。

我该怎么做?

您可以使用 NSCalendarNSDateComponents

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

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