Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails timezone is wrong when shown

I have a problem when showing a datetime object from the database. The time is correctly set in when storing the object, but when it is fetched from db and shown to user it is shown in UTC

environment.rb

config.time_zone = 'Copenhagen'

This is what is saved using Time.now or Time.zone.now

2010-07-08 13:59:50 +0200

This is what is shown to the user when using the html helper <%=h ff.date_registered %> 2010-07-08 11:59:50 UTC

like image 614
Flexo Avatar asked May 29 '26 03:05

Flexo


1 Answers

Try Time.current. Time.now is returned in UTC -5 always. ActiveRecord instructs Time.current based on your config.time_zone. See this github issue for more information, https://github.com/rails/rails/issues/3128#issuecomment-2195751

like image 132
Mike Avatar answered May 30 '26 22:05

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!