Bitwise
Bitwise

Reputation: 8451

Display abbreviated time zone from string representation

Let's say I have this string saved as a "time_zone" on my "Event" model.

 "America/Chicago"

I'd like to convert that value to the abbreviated representation. So, that would ideally look like this

"EST"

Is there a standard Rails method for making this conversion? I haven't been able to find one.

Upvotes: 0

Views: 70

Answers (2)

Moustafa Sallam
Moustafa Sallam

Reputation: 1132

Use ActiveSupport

ActiveSupport::TimeZone["America/Chicago"].tzinfo.current_period.abbreviation.to_s

"CST"

Upvotes: 0

Bitwise
Bitwise

Reputation: 8451

Looks like we can use:

Time.now.in_time_zone(zone).strftime('%Z')

Upvotes: 1

Related Questions