编辑来自开发人员在 github 上的问题响应
node-postgres 团队很久以前就决定在提取日期和日期时间时将没有时区的日期和日期时间转换为本地时间。这与我们过去挖掘的一些文档是一致的。如果您在这里解决旧问题,您会找到讨论。
好消息是它很容易覆盖此行为并返回您认为合适的日期。
这里有关于如何执行此操作的文档:
https ://github.com/brianc/node-pg-types
甚至可能有一个模块可以将postgres中的日期转换为您想要的任何时区(我猜是utc)。如果没有……那是写一篇并与大家分享的好机会!
原始消息
看起来这是 pg-module 中的一个问题。我是 JS 和 node 的初学者,所以这只是我的解释。
解析日期(没有时间部分)时,假定为本地时间。pg\node_modules\pg-types\lib\textParsers.js
if(!match) {
dateMatcher = /^(\d{1,})-(\d{2})-(\d{2})$/;
match = dateMatcher.test(isoDate);
if(!match) {
return null;
} else {
//it is a date in YYYY-MM-DD format
//add time portion to force js to parse as local time
return new Date(isoDate + ' 00:00:00');
但是当 JS 日期对象被转换回字符串getTimezoneOffset时被应用。pg\lib\utils.js 。function dateToString(date)