Ruby code to get the date of next Monday (or any day of the week)
Asked Answered
F

8

57

Given an input of, for example,

day = 'Monday'

how can I calculate the date of the next day?

def date_of_next(day)
  ...
end
Formularize answered 28/10, 2011 at 14:11 Comment(0)
L
106
require 'date'

def date_of_next(day)
  date  = Date.parse(day)
  delta = date > Date.today ? 0 : 7
  date + delta
end

Date.today
#=>#<Date: 2011-10-28 (4911725/2,0,2299161)>
date_of_next "Monday"
#=>#<Date: 2011-10-31 (4911731/2,0,2299161)>
date_of_next "Sunday"
#=>#<Date: 2011-10-30 (4911729/2,0,2299161)>
Lanctot answered 28/10, 2011 at 14:26 Comment(1)
Date.parse(day) awesome trick... before this answer I didn't know.. +1Favata
C
54

For anyone like me who came here looking for a solution in Rails to this problem, as of Rails 5.2 there is a much easier method to do this.

For anyone (like the original poster) not specifically using Rails, this functionality is available in the ActiveSupport gem.

To find the next occurring day of a week, we can simply write something like Date.today.next_occurring(:friday).

See the documentation for more details.

Coffman answered 6/5, 2018 at 15:26 Comment(0)
T
24

I know this is an old post, but I came up with a couple of methods to quickly get the previous and next day of the week.

# date is a Date object and day_of_week is 0 to 6 for Sunday to Saturday

require 'Date'

def get_next_day(date, day_of_week)
  date + ((day_of_week - date.wday) % 7)
end

def get_previous_day(date, day_of_week)
  date - ((date.wday - day_of_week) % 7)
end

puts today = Date.today
# 2015-02-24

puts next_friday = get_next_day(today, 5)
# 2015-02-27

puts last_friday = get_previous_day(today, 5)
# 2015-02-20
Tint answered 24/2, 2015 at 23:11 Comment(4)
in your example you used "5" as the day of the week, but wrote monday as the variable name. I think you meant to use "friday"Cloudberry
@PETER: Thanks for catching thatTint
If it's currently Monday, won't get_next_day(date, 1) return the current date?Bratislava
@MiloP Yes it will. That was the desired behavior for my project. The name of the functions are a little misleading.Tint
A
18

Rails >= 5.2.3

Date.current.next_occurring(:monday)

https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-next_occurring

Aggressor answered 27/11, 2019 at 15:9 Comment(0)
P
14

For stuff like this I rely on the chronic library.

The Ruby code would be:

def date_of_next(day)
    Chronic.parse("next #{day}")
end
Publicize answered 28/10, 2011 at 14:15 Comment(0)
C
14

If you are using rails you can use Date.today.sunday for Sunday or Date.today.monday for Monday. And then Date.today.sunday - 1.day for Saturday etc.

Credent answered 21/5, 2015 at 8:11 Comment(4)
Date.today is only available if you are using Rails or have included ActiveSupport. Even then Date.today.monday gives undefined methodBillingsgate
This actually works for me. irb(main):001:0> Date.today.monday => Mon, 17 Aug 2015Boner
Date.today.monday should work, but such a method only exists for #monday, not the other days of the week. rubydoc.info/docs/rails/4.0.0/DateAndTime/…Chungchungking
Keep an eye, this approach doesn't returns you the next monday, but the monday for this weekJamshedpur
B
5

A Rails 4-compatible solution:

(Date.today + 1.week).beginning_of_week(:monday)

where you can provide the day you'd like to find as a symbol argument; the default is :monday.

Note that this will find the next occurrence of the given day - if today is Monday and you're looking for the next Monday, it will return the Monday one week from today.

Some equivalent ways to do it:

1.week.from_now.beginning_of_week(:monday).to_date

1.week.since(Date.today).beginning_of_week(:monday)

(source: https://apidock.com/rails/Date/beginning_of_week/class)

Bratislava answered 14/12, 2018 at 22:45 Comment(2)
or 1.week.from_now.beginning_of_week(:monday)Topsyturvy
@Topsyturvy That would return a TimeWithZone, while the OP is looking for a date. 1.week.from_now.beginning_of_week(:monday).to_date would do it.Bratislava
F
0

I know this is a Ruby question, but I think many people using Rails are gonna find this. Many answers use Date.today, which is server time and is usually not a good idea to use if you're on Rails. Recommended to incorporate user's time zone:

DateTime.now.in_time_zone(my_user.time_zone).next_occurring(:monday)
Faris answered 11/1 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.