How to initialize DateTime for today, but with a specific time?

How easy is it to initialize a DateTime until today, but with a specific time?

d = DateTime.(??) #desired resulting time => 09 April 2011 9:46 PM 
+6
source share
3 answers

Thanks for your reply @ Vadim. I had to look a little harder. I was able to do this:

 DateTime.now.change(:hour => 21, :minute => 46) 
+8
source

DateTime#change must be from some external library. It does not work in the default setting. For cases when you do not have such a library, for example:

 Date.today.to_time+(21*3600+46*60+53) 

can work.

+3
source

Time.new

 d = Time.new(2011, 4, 9, 21, 46) 

or if you want only the current time

 d = Time.new 

or

 d = Time.now 
+2
source

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


All Articles