How to display the time in user's timezone

后端 未结 8 1222
深忆病人
深忆病人 2020-11-30 05:42

I am using rails 3.0.5 and I have created_at and updated_at stored in UTC. Now I want to display the created_at time in users\' timezone. I believe it is possible to pick us

8条回答
  •  感动是毒
    2020-11-30 06:18

    Assuming that the value you want displayed is coming from the database, :ie started_at and is (as is the default) stored in UTC.

    If you have the user's timezone as an offset you can also localize the time by doing:

    started_at.in_time_zone(-2)
    => Mon, 24 Feb 2014 23:07:56 GST -02:00
    

    Which then can be munged in all sorts of way to get the parts you want:

    started_at.in_time_zone(-2).yesterday
    => Sun, 23 Feb 2014 23:07:56 GST -02:00
    
    started_at.in_time_zone(-2) + 3.days
    => Thu, 27 Feb 2014 23:07:56 GST -02:00
    

提交回复
热议问题