Node.js和Postgres中的日期转换 [英] Date conversion in nodejs and postgres

查看:96
本文介绍了Node.js和Postgres中的日期转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
  }

推荐答案

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

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.

这篇关于Node.js和Postgres中的日期转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆