Rails Previous Sunday in Relation to Any Datetime

Rails Previous Sunday in Relation to Any DateTime

class DateTime
def previous_sunday
self - wday
end
end

…but this is so easy, there's no reason to monkey patch it into the class :)

irb(main):001:0> require 'date'; now = DateTime.now
#=> #<DateTime: 2011-11-10T21:10:04-07:00 (…)>
irb(main):002:0> sunday = now - now.wday
#=> #<DateTime: 2011-11-06T21:10:04-07:00 (…)>

The wday (weekday) will be 0 for any Sunday, and so no change to the date will take place.

Set a datetime for next or previous sunday at specific time

Start with this snippet. In rails, given a date/time object d, the expression

d - wday.day

Gives the last Sunday not after that date. i.e. if the day itself is a Sunday the result will be d.

Then to get the next sunday:

sunday1 = d - wday.day
sunday2 = sunday1 + 7.day

And to set the times for both:

sunday1 = (d - wday.day).change(:hour => 8,:min => 30)
sunday2 = (sunday1 + 7.day).change(:hour => 20)

No need for any external modules. Rails (but not core ruby!) has all the date/time manipulation functionality you need.

Note how the change function works: if you pass it :hour only, it will reset the minute and second to 0. If you pass it :hour and :minute, it will reset only the second. See the docs for details.

One last thing: be mindful of what time zone you are using.

Ruby Year-Month-Date of last Sunday

In rails 4:

most_recent_sunday = Time.now.sunday.to_s

last_sunday = Time.now.last_week.sunday.to_s

To get it to the format you are after:

DateTime.parse(most_recent_sunday).strftime("%Y-%m-%d")

http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/sunday
http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/last_week

Ruby/Rails - Get the previous Friday? (Friday of last week)

Here's a way to do it without any conditionals. This will accept a Date, Time, or DateTime object.

def prior_friday(date)
days_before = (date.wday + 1) % 7 + 1
date.to_date - days_before
end

Now you can do any of these:

>> prior_friday(Date.today)
=> Fri, 24 Mar 2017
>> prior_friday(Date.tomorrow)
=> Fri, 31 Mar 2017
>> prior_friday(Date.new(2017,4,4))
=> Fri, 31 Mar 2017
>> prior_friday(Time.now)
=> Fri, 24 Mar 2017
>> prior_friday(DateTime.tomorrow)
=> Fri, 31 Mar 2017
>> prior_friday(1.year.from_now)
=> Fri, 30 Mar 2018

For different days of the week, change the integer added to date.wday before the mod is applied:

def prior_monday(date)
days_before = (date.wday + 5) % 7 + 1
date.to_date - days_before
end

Or make a universal method:

def prior_weekday(date, weekday)
weekday_index = Date::DAYNAMES.reverse.index(weekday)
days_before = (date.wday + weekday_index) % 7 + 1
date.to_date - days_before
end

Examples run on Saturday, August 4, 2018:

>> prior_weekday(Date.today, 'Saturday')
Sat, 28 Jul 2018
>> prior_weekday(Date.today, 'Friday')
Fri, 03 Aug 2018
>> prior_weekday(Date.tomorrow, 'Friday')
Fri, 03 Aug 2018
>> prior_weekday(Date.tomorrow, 'Saturday')
Sat, 04 Aug 2018
>> prior_weekday(Date.tomorrow, 'Sunday')
Sun, 29 Jul 2018

How to calculate next, previous business day in Rails?

As far as I understand, this is what you are looking for? (tested it)

require 'date'
def next_business_day(date)
skip_weekends(date, 1)
end

def previous_business_day(date)
skip_weekends(date, -1)
end

def skip_weekends(date, inc = 1)
date += inc
while date.wday == 0 || date.wday == 6
date += inc
end
date
end

You can test it as follows:

begin
t = Date.new(2009,9,11) #Friday, today
puts "Today: #{Date::DAYNAMES[t.wday]} #{Date::MONTHNAMES[t.mon]} #{t.day}"
nextday = next_business_day(t)
puts "Next B-day: #{Date::MONTHNAMES[nextday.mon]} #{nextday.day}"
previousday = previous_business_day(nextday)
puts "back to previous: #{Date::MONTHNAMES[previousday.mon]} #{previousday.day}"
yesterday = previous_business_day(previousday)
puts "yesterday: #{Date::MONTHNAMES[yesterday.mon]} #{yesterday.day}"
end

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

end_of_week returning different dates

UPD The answer is changed because of misunderstanding of original question.

▶ dt = DateTime.parse "Sun, 12 Apr 2015"
#=> Sun, 12 Apr 2015 00:00:00 +0000
▶ [
▷ (dt - 1.week).beginning_of_week(:sunday),
▷ (dt - 1.week).end_of_week(:sunday)
▷ ]
#=> [
# [0] Sun, 05 Apr 2015 00:00:00 +0000,
# [1] Sat, 11 Apr 2015 23:59:59 +0000
#]

In Ruby, how to compute the number of days until next Sunday?

7-today.wday # Days until next Sunday

More generally, to answer this question for a general day d = 0,1,2,3,4,5,6 for Sun,Mon,Tue,Wed,Thu,Fri,Sat respectively:

DAYS_PER_WEEK = 7
(d-today.wday-1) % DAYS_PER_WEEK + 1


Related Topics



Leave a reply



Submit