为什么Rails总是以正确的时间显示datetime为1/1/2000 [英] Why does Rails always display a datetime as 1/1/2000 - with the correct time
问题描述
这个是让我坚强!数据正确地存储到db(sqlite3)中。但是,当我从记录中显示日期时,Rails似乎将其强制为1/1/2000 - 正确的时间。换句话说,如果我指定为日期时间:2009年12月31日6:00 PM,sqllite3实际上将显示2009-12-31 18:00:00。但是,Rails将显示2000年1月1日06:00 PM(保持正确的时间)的价值。
我已经创建了虚拟属性来处理日期格式(看起来正常工作)。而且,我设置了我的时区:
config.time_zone ='东部时间(美国和加拿大)'
我不得不相信这是一个简单的事情...它完全驱动我坚果!
谢谢!
John
我有同样的问题,使用postgres,感谢user145110的建议使用DateTime类而不是时间。这对我有用我只会补充说,尽管我不得不改变我的迁移使用DateTime,但我仍然可以使用Time和它的方法在与DateTime值进行比较的测试中。换句话说,这个原始迁移:
t.time:start_time
/ pre>
...并使用时间进行样本分配:
start_time = Time.now.beginning_of_day
...它保存好了,但是稍后阅读,年改为2000年。也许这是一个旧的Y2K错误。大声笑。相反,我只是更改迁移:
t.datetime:start_time
pre>
..我所有的代码都有效。即使这样的作业:
start_time = Time.zone.now.beginning_of_year.beginning_of_day
尽管如此,我无法感觉到我失去了一些东西。别人用时间来操纵日期吗?
This one is driving me nuts!! The data is getting stored into the db (sqlite3) correctly. However, when I display the date from the record, Rails appears to coerce it to 1/1/2000 - with the correct time. In other words, if I specify this as the date time: December 31, 2009 6:00 PM, sqllite3 will in fact show 2009-12-31 18:00:00. But.... Rails will display the value has January 1, 2000 06:00 PM (keeping the correct time).
I have created virtual attributes to handle date formatting (which appear to work correctly). And, I have set my time zone to:
config.time_zone = 'Eastern Time (US & Canada)'
I have to believe this is something simple...It is totally driving me nuts!!
Thanks!
John
解决方案I had the same problem, using postgres, and thanks user145110 for the suggestion to use the DateTime class instead of Time. That worked for me. I would only add that though I had to change my migration to use DateTime, I was still able to use Time and its methods in tests that compared against DateTime values. In other words, with this original migration:
t.time :start_time
...and sample assignment using Time:
start_time = Time.now.beginning_of_day
...it saved ok, but upon reading it later, the year was changed to 2000. Maybe it's an old Y2K bug. LOL. Instead, I simply changed the migration:
t.datetime :start_time
..and all my code worked. Even assignments like this work:
start_time = Time.zone.now.beginning_of_year.beginning_of_day
I can't help feeling like I missing something, though. Do others use Time to manipulate dates?
这篇关于为什么Rails总是以正确的时间显示datetime为1/1/2000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!