Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 week from now at 8 o'clock

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?

like image 784
Ruslan Avatar asked Oct 05 '17 13:10

Ruslan


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)
like image 163
Tom Lord Avatar answered Nov 03 '22 10:11

Tom Lord


[...] 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 image 7
Stefan Avatar answered Nov 03 '22 10:11

Stefan


Like this, for example:

(DateTime.now + 1.week).beginning_of_day + 8.hours
like image 4
Sergio Tulentsev Avatar answered Nov 03 '22 10:11

Sergio Tulentsev