Tom Lehman
Tom Lehman

Reputation: 89243

Get a Time object representing a given time on a given date in Ruby

What's the easiest way to give a Time object representing a given time (say 5pm) on a given date (say 1/1/2010) in a given time zone (say EST)?

Upvotes: 1

Views: 172

Answers (1)

tadman
tadman

Reputation: 211610

I'm a big fan of Time.parse, although Time.mktime is also an option:

Time.parse("1/1/2010 5:00pm EST") # => Fri Jan 01 17:00:00 -0500 2010
Time.mktime(2010, 1, 1, 17, 00) # => Fri Jan 01 17:00:00 -0500 2010

Note that Time.mktime always renders results using the local timezone, so Time.parse is more flexible in that regard.

Upvotes: 3

Related Questions