I need the utc offset from the timezone specified.
8 Answers
If you need to take daylight savings time into account, you can use something like this:
Time.now.in_time_zone('America/New_York').utc_offset

- 2,351
- 1
- 16
- 18
-
2Thank you. This got me the correct offset where `Time.zone.utc_offset` did not. – Henrik N Aug 27 '15 at 12:55
-
@GregDubicki It's in Rails's `ActiveSupport::TimeWithZone` – Dorian Dec 02 '16 at 18:24
-
2This answer works better than `Time.zone.utc_offset`, which inexplicably doesn't account for Daylight Saving Time. – Andrew Smith Sep 08 '20 at 19:11
require 'time'
p Time.zone_offset('EST') #=> -18000 #(-5*60*60)

- 128,817
- 21
- 151
- 173

- 79,051
- 16
- 138
- 171
-
10
-
1Don't forget that in order for this to work properly, you have to set `config.time_zone = 'Berlin'` and `config.active_record.default_timezone = 'Berlin'` in application.rb (in my case, without that it didn't reflect daylight savings adjustments correctly) – Dennis Hackethal Jul 16 '13 at 13:21
-
1@Charles, I had to change `config.active_record.default_timezone` to `:local` instead of `'Santiago'`, otherwise every DateTime field change to nil. Is `Time.zone_offset` going to deal with daylight savings correctly? – Sebastialonso Aug 17 '14 at 15:47
-
1Lucas' comment to the answer is the best... what steenslag proposes gets borked by daylight savings time if you rely on that offset changing when that happens. (EST vs EDT in this instance). – Wes Johnson Mar 05 '15 at 18:26
-
`Time.now.utc_offset` also works with DST while `Time.zone.utc_offset` does not. – mb21 Apr 20 '18 at 19:39
-
Time.now.in_time_zone('Europe/London').formatted_offset
# => "+01:00"
For a string arguably more usable...

- 1,118
- 12
- 17
my_date_time = DateTime.new(2011, 3, 29, 20)
#=> Tue, 29 Mar 2011 20:00:00 +0000
#will return the same time but with another offset
my_date_time.change(:offset => "+0100")
#=> Tue, 29 Mar 2011 20:00:00 +0100
#will return time for other offset
my_date_time.in_time_zone(-1)
#=> Tue, 29 Mar 2011 19:00:00 -0100

- 82,987
- 33
- 217
- 237
If you are storing a user's timezone as a Time.zone.name
string such as 'Eastern Time (US & Canada)'
as outlined in http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html and want to get the timezone offset you can do this:
ActiveSupport::TimeZone.new(user.time_zone).utc_offset / 3600
# => -5

- 1,769
- 1
- 13
- 12
-
1Keep in mind, this solution only returns the canonical offset and doesn't contemplate daylight saving time. So, to be safe, is better to use `Time.now.in_time_zone(user.time_zone).utc_offset / 3600` – Alter Lagos Mar 16 '21 at 20:21
I use Time.zone.utc_offset / 1.hour
to get the offset in hours (e.g.: -8
for San Francisco)

- 22,759
- 8
- 120
- 116
If you have a ActiveSupport::TimeWithZone
object, you can call time_zone.utc_offset
on it. So, for example, Time.zone.now.time_zone.utc_offset
.
EDIT: You can also use the gmt_offset
instance method on normal Time
objects.

- 3,511
- 27
- 35
Just to complete the loop here, right now I use:
@course.start.utc_offset/60/60
in order to get the # of hours needed. Jquery countdown needs just -5 or +3 in their timezone
argument.
Your welcome google.

- 9,489
- 5
- 46
- 56