: monday", but "DateTime.current.wday == 0" is Sunday DateTime.current.wday starts the countdown of the week fro...">

"DateTime.beginning_of_week =>: monday", but "DateTime.current.wday == 0" is Sunday

DateTime.current.wday starts the countdown of the week from Sunday.

I want weeks to count from Monday.

I do not understand this discrepancy.

How to get a working day in numerical representation today?

+4
source share
2 answers

I believe that traditionally weeks are considered starting from Sunday on the first day of the week. If you want to present a week starting from another day, for example, on Monday, you can do something like

(DateTime.current.wday + 6) % 7

Other than that, I believe there is strftime to get what you are looking for, but it will be returned as a string.

+3

wday Ruby 0 6, 0 - , .

1 7 ( 1 - ), strftime('%u'). '%w' 0 6 ( 0 - ).

2.0.0-p247 :001 > Date.new(2014,5,4).strftime('%u')
 => "7" 
2.0.0-p247 :002 > Date.new(2014,5,4).strftime('%w')
 => "0" 
2.0.0-p247 :003 > Date.new(2014,5,4)
 => Sun, 04 May 2014 

, , , . Rails Date.beginning_of_week=(week_start), , .

+3

Source: https://habr.com/ru/post/1538732/


All Articles