Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the number week of the year start with 1 or 0 depending on the year?

Tags:

ruby

iso8601

Why does the number of the week of the year start with 1 in 2017 and 0 in 2018?

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
like image 371
devsaki Avatar asked Jan 03 '18 20:01

devsaki


1 Answers

From the Ruby docs

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)

So it seems that Ruby identifies the "first week" (week 1) as starting with the first Sunday of the year. Anything that happens to come before that exists in week 0. 2017 happened to start on a Sunday, so the first day started the first week. However, 2018 started on a Monday, so week 1 of 2018 will start on January 7th, the first Sunday of the year.

like image 129
Silvio Mayolo Avatar answered Nov 02 '22 22:11

Silvio Mayolo