Mohamed Sami
Mohamed Sami

Reputation: 916

Ruby week number results

Why when I use

Time.now.strftime('%Y%W')

or

Date.today.strftime('%Y%W')

they return 201912 while it should be 201913 as we are in week 13, not 12.

How to get the current week number?

Upvotes: 0

Views: 229

Answers (2)

jerhinesmith
jerhinesmith

Reputation: 15502

If you look in the documentation, it says this:

Week number: The first week of YYYY that 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)
  %W - Week number of the year. The week starts with Monday. (00..53)

January 1st, 2019 was on a Tuesday, so that would have been week 0 - making today week 12.

Upvotes: 5

Steven Aguilar
Steven Aguilar

Reputation: 3049

Time.now.strftime('%Y%V')

Would give you the output your looking for.

%V - Week number of the week-based year (01..53)

You have to look for the week-based year.

Upvotes: 2

Related Questions