Date conversion in nodejs and postgres

倾然丶 夕夏残阳落幕 提交于 2019-12-13 03:27:05

问题


I have column birthday in postgres with type date, I receive a unixtimestamp from front-end like 716500800. When I am saving it to postgresql, it seems like it is converting based on my local time zone. I don't understand what I should do, here is a code

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
  }

回答1:


So I set timezone on server and db to UTC. From front-end I get timezone and chande db field to timestamptz (with time zone). Now I operate with all dates in UTC and show/get from client with time zone.



来源:https://stackoverflow.com/questions/49852123/date-conversion-in-nodejs-and-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!