[英]How to "use" TIMESTAMP postgres data type
I have saved a datetime value created by Luxon into a postgres database column, of type TIMESTAMP(3).我已将 Luxon 创建的日期时间值保存到 TIMESTAMP(3) 类型的 postgres 数据库列中。 I want to then use that value, convert it into other time zones, etc. However, I can't seem to figure out how to "use" it.然后我想使用该值,将其转换为其他时区等。但是,我似乎无法弄清楚如何“使用”它。
I created the object using the following我使用以下方法创建了 object
const { DateTime } = require("luxon");
const myDate = DateTime.now().toUTC().toISO()
I then inserted it into a postgres database, into a column of type TIMESTAMP(3).然后我将它插入到 postgres 数据库中,插入 TIMESTAMP(3) 类型的列中。
I extract it out of the database using a query.我使用查询将其从数据库中提取出来。 When I log it, it says its value is:当我记录它时,它说它的值是:
console.log(extracted_date); //=> "2021-12-27T09:57:16.184Z"
console.log(typeof extracted_date); //=> object
// The following return "unparsable" or undefined objects
DateTime.fromISO(extracted_date);
DateTime.fromObject(extracted_date);
I can find plenty of tutorials about how to insert dates into sql, but nothing on how to take that date object and actually do something with it.我可以找到很多关于如何将日期插入 sql 的教程,但没有关于如何获取该日期 object 并实际使用它的教程。 I want to, for instance, convert its time zone.例如,我想转换它的时区。
To use that date object you can initiate a new Date, like so:要使用该日期 object 您可以启动一个新日期,如下所示:
console.log(extracted_date); //=> "2021-12-27T09:57:16.184Z"
const javascriptDate = new Date(extracted_date);
Than you can use it directly or with the luxon library.比您可以直接使用它或与 luxon 库一起使用。
console.log(javascriptDate.toGMTString()); // => "Mon, 27 Dec 2021 09:57:16 GMT"
console.log(javascriptDate.toISOString()); // => "2021-12-27T09:57:16.184Z"
console.log(javascriptDate.valueOf()); // => 1640599036184
This object core is actually, a value that represents milliseconds since 1 January 1970 UTC, and added to that are time and string parsing functions, generally speaking.这个 object 内核实际上是一个表示自 1970 年 1 月 1 日 UTC 以来的毫秒数的值,一般来说,添加了时间和字符串解析功能。
If you want to read more about timestamp, here are some references:如果您想了解有关时间戳的更多信息,请参阅以下参考资料:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.