scientiffic scientiffic - 4 months ago 19
Ruby Question

Get UTC offset in Ruby

I'm trying to get the UTC offset for a datetime. The datetime is saved as:


Fri, 31 May 2013 15:19:08 EDT -04:00


What I'd like to get is
"-04:00"
. When I do
object.utc_offset
, I get
-14400
. What function should I use?

Answer

You are looking for strftime:

Time.now.strftime("%:z")

That will give you the timezone such as "-04:00".