1 week from now at 8 o'clock

Viewed 4002

I'd like to get a DateTime that is 1 week from now at 8 in the morning.

Getting one week is easy:

DateTime.now + 1.week

How do I set the resulting date to a specific time?

3 Answers

There are many ways to achieve this. Here's one:

Time.parse('8am') + 1.week

Here's another:

DateTime.now.beginning_of_day + 1.week + 8.hours

Or how about:

1.week.from_now.beginning_of_day + 8.hours

Or even:

DateTime.now.advance(days: 7).change(hour: 8)

[...] 1 week from now at 8 in the morning

I'd express that via:

1.week.from_now.change(hour: 8)
#=> Thu, 12 Oct 2017 08:00:00 CEST +02:00

change automatically sets "smaller" time units (min, sec, etc.) to 0.

Like this, for example:

(DateTime.now + 1.week).beginning_of_day + 8.hours
Related