简体   繁体   English

从 postrgresql 检索的节点日期正在转换为不需要的格式

[英]Node date retrieved from postrgresql is being converted to an undesired format

I have a pg select我有一个 pg select

db.query("SELECT * from users where id ="+userId).then(data => {
   console.log(data[0].birth_date)
})

Which returns哪个返回

1997-08-11T22:00:00.000Z

I have prepared a function to parse the data我准备了一个function来解析数据

function filterDate(date) {
console.log("inside")
console.log(date)
var stringDate = date.toString();
var result = stringDate.match(/(?:(?!T).)*/)
console.log(result)
console.log("inside")
return result[0];

} }

Which should return 1997-98-11 Problem is that, whenever the.toString() happens inside filterDate, it preformats the date to an invalid string 'Tue Aug 12 1997 00:00:00 GMT+0200 (GMT+02:00)', .应该返回1997-98-11问题是,每当在 filterDate 中发生 the.toString() 时,它会将日期预格式化为无效字符串'Tue Aug 12 1997 00:00:00 GMT+0200 (GMT+02:00)', . I was handling it in the client side, so I was directly doing it after a res.send, and it was working fine.我在客户端处理它,所以我在 res.send 后直接做它,它工作正常。 But now if i date.toString() in the server side it reformats it to a weird format.但是现在,如果我在服务器端使用 date.toString() ,它会将其重新格式化为一种奇怪的格式。 How do I avoid it and mantain the first format?如何避免它并保持第一种格式?

Using moment npm使用力矩 npm

  function filterDate(date) {
    var stringDate = moment(date).format('YYYY-MM-DD').toString();
    var result = stringDate.match(/(?:(?!T).)*/)
    return result[0];
  }

And then接着

deathDate = moment(filterDate(birth_date)).add(yearsToLive, 'years')

Will assign the proper format as YYYY-MM-DD将分配正确的格式为 YYYY-MM-DD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM