为什么一年中的第几周根据年份以 1 或 0 开头?
Why does the number week of the year start with 1 or 0 depending on the year?
为什么2017年的周数从1开始,2018年的周数从0开始?
Date.strptime('2017-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %U') #2017-01-01 01
Date.strptime('2018-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %U') #2018-01-01 00
Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to %U
or %W
). The days in the year before the first week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
因此 Ruby 似乎将 "first week"(第 1 周)确定为从一年中的第一个星期日开始。任何发生在它之前的事情都存在于第 0 周。2017 年恰好从星期日开始,所以第一天从第一周开始。但是,2018 年是从星期一开始的,所以 2018 年的第 1 周将从 1 月 7 日开始,即一年中的第一个星期日。
要根据 ISO-8601 显示周数,请使用 %V
:
# %V - Week number of the week-based year (01..53)
Date.strptime('2017-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %V') #2017-01-01 52
Date.strptime('2018-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %V') #2018-01-01 01
总的来说:
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week').
为什么2017年的周数从1开始,2018年的周数从0开始?
Date.strptime('2017-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %U') #2017-01-01 01
Date.strptime('2018-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %U') #2018-01-01 00
Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to
%U
or%W
). The days in the year before the first week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
因此 Ruby 似乎将 "first week"(第 1 周)确定为从一年中的第一个星期日开始。任何发生在它之前的事情都存在于第 0 周。2017 年恰好从星期日开始,所以第一天从第一周开始。但是,2018 年是从星期一开始的,所以 2018 年的第 1 周将从 1 月 7 日开始,即一年中的第一个星期日。
要根据 ISO-8601 显示周数,请使用 %V
:
# %V - Week number of the week-based year (01..53)
Date.strptime('2017-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %V') #2017-01-01 52
Date.strptime('2018-01-01', '%Y-%m-%d').strftime('%Y-%m-%d %V') #2018-01-01 01
总的来说:
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week').