【问题标题】:Date conversion in nodejs and postgresnodejs和postgres中的日期转换
【发布时间】:2018-09-25 21:17:15
【问题描述】:

我在 postgres 中有类型为 date 的列生日,我从前端收到一个 unixtimestamp,例如 716500800。当我将它保存到 postgresql 时,它似乎正在根据我的本地时区进行转换。我不明白我应该怎么做,这是一个代码

const date = moment.utc(data.birthday * 1000).format();
console.log(date); // 1992-09-15T00:00:00Z it is right date

db.query(
  'UPDATE app_users SET birthday=$1 where id=$2 RETURNING birthday',
  [
    date,
    id
  ],
  (err, bd) => {
    console.log(bd.rows); // birthday: 1992-09-14T20:00:00.000Z
  }

【问题讨论】:

    标签: node.js postgresql momentjs unix-timestamp utc


    【解决方案1】:

    所以我将服务器和数据库上的时区设置为 UTC。从前端我得到 timezone 和 chande db 字段到 timestamptz (带时区)。现在我使用 UTC 中的所有日期进行操作,并使用时区从客户端显示/获取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2022-01-09
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      相关资源
      最近更新 更多