After 1 week at 8 o’clock

I would like to get a DateTime, which will take place in a week in 8 hours in the morning.

Getting one week is easy:

DateTime.now + 1.week

How to set the final date at a specific time?

+4
source share
3 answers

There are many ways to achieve this. Here is one:

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

Here is 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)
+8
source

[...] A week later at 8 am

I would say that through:

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

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

+5
source

Like this, for example:

(DateTime.now + 1.week).beginning_of_day + 8.hours
+4
source

Source: https://habr.com/ru/post/1686963/


All Articles