Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: combine Date and Time objects into a DateTime

Tags:

ruby

Simple question, but I can't find a good or definitive answer. What is the best and most efficient way to combine Ruby Date and Time objects (objects, not strings) into a single DateTime object?

like image 307
Brendon Avatar asked Aug 29 '12 15:08

Brendon


3 Answers

I found this, but it's not as elegant you would hope:

d = Date.new(2012, 8, 29)
t = Time.now
dt = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)

By the way, the ruby Time object also stores a year, month, and day, so you would be throwing that away when you create the DateTime.

like image 74
David Grayson Avatar answered Sep 22 '22 11:09

David Grayson


When using seconds_since_midnight, changes in daylight savings time can lead to unexpected results.

Time.zone = 'America/Chicago'
t  = Time.zone.parse('07:00').seconds_since_midnight.seconds
d1 = Time.zone.parse('2016-11-06').to_date # Fall back
d2 = Time.zone.parse('2016-11-07').to_date # Normal day
d3 = Time.zone.parse('2017-03-12').to_date # Spring forward

d1 + t
#=> Sun, 06 Nov 2016 06:00:00 CST -06:00
d2 + t
#=> Mon, 07 Nov 2016 07:00:00 CST -06:00
d3 + t
#=> Sun, 12 Mar 2017 08:00:00 CDT -05:00

Here's an alternative, similar to @selva-raj's answer above, using string interpolation, strftime, and parse. %F is equal to %Y-%m-%d and %T is equal to %H:%M:%S.

Time.zone = 'America/Chicago'
t = Time.zone.parse('07:00')
d1 = Time.zone.parse('2016-11-06').to_date # Fall back
d2 = Time.zone.parse('2016-11-07').to_date # Normal day
d3 = Time.zone.parse('2017-03-12').to_date # Spring forward

Time.zone.parse("#{d1.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 06 Nov 2016 07:00:00 CST -06:00
Time.zone.parse("#{d2.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 07 Nov 2016 07:00:00 CST -06:00
Time.zone.parse("#{d3.strftime('%F')} #{t.strftime('%T')}")
#=> Sun, 12 Mar 2017 07:00:00 CDT -05:00
like image 38
Alexander Clark Avatar answered Sep 23 '22 11:09

Alexander Clark


Simple:

Date.new(2015, 2, 10).to_datetime + Time.parse("16:30").seconds_since_midnight.seconds

# => Object: Tue, 10 Feb 2015 16:30:00 +0000

You gotta love Ruby!

like image 34
karlingen Avatar answered Sep 22 '22 11:09

karlingen