I'm using Node.js, Postgres, and the node-postgres library. When I try to insert a new record with the current date with code like this:
我正在使用Node.js,Postgres和node-postgres库。当我尝试使用如下代码插入当前日期的新记录时:
client.query('INSERT INTO ideas(date) VALUES($1)', [new Date()], ...);
And this runs fine with no errors. However, when I run a select statement against the database and log the results, the date I'm given appears as:
这样运行正常,没有错误。但是,当我对数据库运行select语句并记录结果时,我给出的日期显示为:
Wed Nov 20 2013 19:00:00 GMT-0500 (EST)
Which is fine, except that when I inserted the record, it was Thursday, November 21. And the time was 5:47, not 7:00 as the output would suggest.
这很好,除了当我插入记录时,它是11月21日星期四。时间是5:47,而不是7:00,因为输出会显示。
I ran the code a few more times, and it stored the same inaccurate date no matter the time, even once the next hour had begun. This leads me to believe that for some reason, it's only storing the date and not the hour or minute. In addition, the fact that the date is only off by one day suggests that the problem might have something to do with the way node-postgres handles dates.
我再运行了几次代码,无论时间是什么,它都存储了相同的不准确日期,即使下一个小时开始也是如此。这让我相信,由于某种原因,它只存储日期而不是小时或分钟。此外,日期只有一天的事实表明问题可能与node-postgres处理日期的方式有关。
I know it's not a problem with Javascript calculating the current date when passing it into the query, because I logged new Date()
and it was accurate, to the date, minute, hour, and second.
我知道Javascript在将它传递给查询时计算当前日期不是问题,因为我记录了新的Date()并且它是准确的,日期,分钟,小时和秒。
Any help on this issue would be greatly appreciated. Thanks!
对此问题的任何帮助将不胜感激。谢谢!
1 个解决方案
#1
5
The problem (thanks to Craig for pointing this out) was that I was using the date
type for my Postgres column, which only records dates, and not times. Once I switched to the timestamptz
type everything worked perfectly!
问题(感谢Craig指出这一点)是我使用Postgres列的日期类型,它只记录日期,而不是时间。一旦我切换到timestamptz类型,一切都很完美!
#1
5
The problem (thanks to Craig for pointing this out) was that I was using the date
type for my Postgres column, which only records dates, and not times. Once I switched to the timestamptz
type everything worked perfectly!
问题(感谢Craig指出这一点)是我使用Postgres列的日期类型,它只记录日期,而不是时间。一旦我切换到timestamptz类型,一切都很完美!