time zone in datetime

  • how do I attach a time zone to some string (the string should have no info of time zone)?

ActiveSupport::TimeZone.new(“Eastern Time (US & Canada)”).parse(“2011-11-14T00:00:00”)

result: Mon, 14 Nov 2011 00:00:00 EST -05:00

FILE_TIME_ZONE=ActiveSupport::TimeZone.new(“Pacific Time (US & Canada)”)

irb(main):014:0> FILE_TIME_ZONE.parse(“2011-02-14T01:00:00”)
=> Mon, 14 Feb 2011 01:00:00 PST -08:00
irb(main):015:0> FILE_TIME_ZONE.parse(“2011-05-14T01:00:00”)
=> Sat, 14 May 2011 01:00:00 PDT -07:00
Note about the Pacific Standard Time (PST) has one hour difference from Pacific daylight time PDT.

Note that if the string ends with “Z” it will treat as the string time as utc time

FILE_TIME_ZONE.parse(“2011-02-14T01:00:00Z”)
=> Sun, 13 Feb 2011 17:00:00 PST -08:00

  • How do I change a time with time zone to utc?

time.utc

example: Time.now.utc

  • how do I convert a UTC DateTime to another time zone?
time.in_time_zone(time_zone)
example: Time.now.in_time_zone("Central Time (US & Canada)")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s