编辑 1:这个答案没有解释 OP 看到的结果。有关更多信息,请参阅评论讨论
编辑 2:我的机器上的结果非常奇特。在 iOS 6 上,week 根本不考虑first day of week
设置System Preferences
:
iOS 6 总是返回:
2000-1 -> 2000 年 1 月 1 日(星期六)
2001-1 -> 2001 年 1 月 6 日(星期六)
即使我这样做[calendar setFirstWeekday: 3]
,[dateFormatter setCalendar: calendar]
也完全不会影响结果(与 iOS 5 不同)!这肯定是 iOS 6 NSCalendar API 中的一个错误。我建议您NSDateComponents
自己使用或计算日期。给出这个答案中的信息应该很容易 - 您可以假设 ISO-8601(第一周至少 4 天,每周从星期一开始),当然,除非您可以询问用户的偏好。
原始答案
这是正在发生的事情:
在您的语言环境中,一周中的最少天数是5
并且一周从星期六开始。
假设这是真的,您的第一周将是5th Jan
(Sat) - 11th Jan 2013
。你猜对了,第 18 周将是:4th May 2013
.
为什么?因为 Apple 遵循Unicode Technical Standard #35 版本 tr35-25(在 iOS 6+ 上)。
F.4 Week of Year
Values calculated for the Week of Year field range from 1 to 53 for the Gregorian calendar
(they may have different ranges for other calendars). Week 1 for a year is the first week
that contains at least the specified minimum number of days from that year. Weeks between
week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52
or 53 (if needed). For example, January 1, 1998 was a Thursday. If the first day of the
week is MONDAY and the minimum days in a week is 4 (these are the values reflecting ISO
8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and
ends on January 4, 1998. However, if the first day of the week is SUNDAY, then week 1 of
1998 starts on January 4, 1998, and ends on January 10, 1998. The first three days of 1998
are then part of week 53 of 1997.
此外,YYYY-w
是正确的格式。