Changing the first day of week in rails

Nowadays the method accepts the start day param:

beginning_of_week(start_day = :monday)

Note: param is supported from Rails 4.2.7 only


I've managed to make a pull request into rails and now you can pass symbol argument to beginning_of_week method. For example beginning_of_week(:sunday) will give you a Sunday assuming that weeks starts on Sunday. The same for end_of_week method. But you have to wait until rails 3.2 release in case you are not on the bleeding edge.

See this for more info: https://github.com/rails/rails/pull/3547


You can work within the api by doing the following:

ree-1.8.7-2011.02 :004 > DateTime.now.beginning_of_week
 => Mon, 30 May 2011 00:00:00 -0400 
ree-1.8.7-2011.02 :005 > DateTime.now.beginning_of_week.advance(:days => -1)
 => Sun, 29 May 2011 00:00:00 -0400 

EDIT:

Thinking about this the last couple of minutes, there is good reason for Rails approaching it this way. You have to have a default start of the week, and passing this optional parameter in, you can now store the start of the week on say, the User table in your database and allow the user to pick which day of the week to start their week, eg:

first_day_of_calendar_week = {:default => 0, :monday => 0, :sunday => -1 ..}
....
@user = User.find_by_some_attribute(...)
DateTime.now.beginning_of_week.advance(
  :days => first_day_of_calendar_week[@user.choice] || first_day_of_calendar_week[:default])

Add this config to config/application.rb

config.beginning_of_week = :sunday