不同的日期保存到数据库 - 错误的时区 [英] Different date saved to database - wrong time zone

查看:502
本文介绍了不同的日期保存到数据库 - 错误的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Fri Sep 13 2013 08:30: 33 GMT-0400(EDT)

在将该日期保存到数据库后,我看到在数据库中有不同的值:

  2013-09-13 12:00:00.000000 
pre>

因此,日期会自动转换为UTC并保存为数据库。如何将正确的日期保存到数据库?我不想硬编码:)



谢谢。

解决方案

默认情况下,rails应用程序时区是UTC。每当保存记录时,日期,日期时间,时间字段都保存为DB的时区,但是当它们在模型中被提取时,将被转换回UTC。



所以,在你的情况下,在application.rb中设置你的应用程序时区,就像这样

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

记录从数据库中获取,它将始终转换为CST时区,与DBs时区无关。



希望这将有所帮助。


When I fetch a date from FullCalendar with Javascript I have the following values:

Fri Sep 13 2013 08:30:33 GMT-0400 (EDT)

After I save that date to database I see that in database there are different values:

2013-09-13 12:00:00.000000

So, date is automatically converted to UTC and saved like that into database. How to save the correct date into database? I don't want to hardcode it :)

Thank you.

解决方案

By default, the rails app timezone is UTC. Whenever the record is saved the date, datetime, time fields are saved with DB's timezone but when they are fetched back in models it is converted back to UTC.

So, in your case set your application timezone in application.rb like this

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

Now, whenever the records are fetched from database it will always be converted to CST timezone irrespective of DBs timezone.

Hope this will help.

这篇关于不同的日期保存到数据库 - 错误的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆