week number of the year, kinda

Viewed 21466

Preferably in ruby, but the logic would be good enough...

I need the week number of the year given that the week is non-standard. So, say you define a week as Saturday -> Friday. Then, given a date, which week number (1-52) is it?

strftime has %U:

> Time.now.strftime('%U')
> => "28"

...but that of course assumes a standard Sunday -> Saturday week.

5 Answers

If you want a ISO week number use %V according strftime documentation

Example:

require 'date'

puts DateTime.parse('2018-12-30 23:59:59').strftime('%G-%V')
puts DateTime.parse('2018-12-31 00:00:00').strftime('%G-%V')

Result:

2018-52
2019-01

Note that it was used %G for year and not %Y.

Related