Search code examples
node.jspostgresqlmomentjsunix-timestamputc

Date conversion in nodejs and postgres


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
  }

Solution

  • 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.