Gangmax Blog

Get time from different time zone

Convert foreign time zone to local time zone:

First, get the time zone offset by googling it. For example: “BST“ offset is “+0100”.

Then run the following lines in “irb”:

1
2
3
4
> require 'time'
=> true
> Time.parse('2012-05-25 08:00 +0100')
=> 2012-05-25 15:00:00 +0800

Convert local time zone to foreign time zone:

First, get the target time zone’s time zone offset, For an example, I want to know what’s the EDT time for my local time “2012-05-12 17:50:22 +0800”. The “EDT“ time zone offest is “-0400”, then run the following lines in “irb”:

1
2
3
4
> require 'time'
=> true
> Time.parse('2012-05-23 17:50:22').utc + (60*60*-4)
=> 2012-05-23 05:50:22 UTC

Actually this is a workaround to only get the number, so just ignore the “UTC” in the result.

Comments