Borbat Borbat - 2 months ago 21
Ruby Question

'Time.now' not showing the right time

I want to use

Time.now.strftime("%d/%m/%Y %H:%M")
to show the current time in my timezone, but it seems to be delayed for two hours. It shows:
28/08/2017 06:36
when it is
08:36
. How can I adjust the timezone to show the right time?

Answer Source

Pure Ruby Way:

> Time.now.utc.localtime("+05:30").strftime("%d/%m/%Y  %H:%M")
#=> "28/08/2017  12:41"

where +05:30 (IST) is the offset of the particular zone

Note: Time.now.getlocal.zone will return your local zone


Using ActiveSupport:

>  require 'active_support/time'
#=> true 
> Time.now.in_time_zone('US/Eastern').strftime("%d/%m/%Y  %H:%M")
#=> "28/08/2017  03:17"