Last Updated: February 25, 2016
·
748
· rshetty

Get Time in Any Time Zone

You can get the current time of any Time zone in your rails Application.
Rails provides methods for Time Conversions.
Here is a snippet to convert the Time zone to Pacific Time (USA)

Time.now.utc.in_time_zone('Pacific Time (US & Canada)')

This returns the Time according to the Pacific US Timing.

You can also make your application use the specific time zone by setting this in your environment.rb file

config.time_zone = 'UTC'

or

config.time_zone = 'Pacific Time (US & Canada)'

And one more thing, to get all the available Time Zones Try this in your rails console

include ActiveSupport
TimeZone.all

This will list all the available Time zones. Set anyone in your rails application according to your needs and voila you are done!!!

That's it.

Happy Hacking...