开发者社区> 问答> 正文

NSDateFormatter的yyyy和YYYY到底什么区别

问题如题目
我在文档里看到

A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
但是这个Week of Year和普通的到底什么区别,求举例

展开
收起
a123456678 2016-07-20 14:27:20 2331 0
1 条回答
写回答
取消 提交回答
  • 在stackoverflow 找到了这样的回答:

    Both results are correct, the difference is because the DateUtils.WeekOfTheYear function uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday.

    Check this explanation about the Weeks Numbers in the ISO 8601 format.

    If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year.

    Also the Embarcadero online help for the DateUtils.WeekOfTheYear function says

    ....if the first calendar day of the year is a Friday, Saturday, or Sunday, then for the first three, two, or one days of the calendar year, WeekOfTheYear returns the last week of the previous year. Similarly, if the last calendar day of the year is a Monday, Tuesday, or Wednesday, then for the last one, two, or three days of the calendar year, WeekOfTheYear returns 1 (the first week of the next calendar year).

    So due which the 01-01-2011 was Saturday this week is considered the week number 52 of the previous year.

    And that also explains why the week number returned for today (19-10-2011) is 42 instead of 43.
    ISO 8601 format的大概意思是:
    如果一月1日是星期一,星期二,星期三或星期四,它是在01周。如果一月1日是星期五,星期六或星期日,它在前一年的52周或53周

    出处:http://stackoverflow.com/questions/78...

    2019-07-17 19:58:49
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载