Kevin Behan
Kevin Behan

Reputation: 496

Ruby on Rails set a specific time for a DateTime object in a Time Zone

I know that calling...

DateTime.now.in_time_zone('Pacific Time (US & Canada)')

will return me a DateTime object in the Pacific Timezone. Is there a way to use in_time_zone to return a DateTime at a specific time instead of just now so that I don't have to convert from UTC time with something like this

 DateTime.new(year, month, day, hour, min).in_time_zone('Pacific Time (US & Canada)')

Upvotes: 3

Views: 4347

Answers (2)

Kevin Behan
Kevin Behan

Reputation: 496

The above is okay but I think this is a better solution for my purposes of going back and forth between multiple time zones seamlessly.

DateTime.now.in_time_zone('Pacific Time (US & Canada)').change({hour: 13, minute: 30}).in_time_zone('Eastern Time (US & Canada)')

Upvotes: 5

Katherine
Katherine

Reputation: 171

You can do this:

my_time_zone = "Pacific Time (US & Canada)"  
ActiveSupport::TimeZone[my_time_zone].parse("2015-02-12 21:57:00")

Which should return a DateTime in the supplied Time Zone.

Upvotes: 0

Related Questions