简体   繁体   English

如何“使用” TIMESTAMP postgres 数据类型

[英]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 以来的毫秒数的值,一般来说,添加了时间和字符串解析功能。


More Info更多信息

In some systems dates are store in the database as the value - date.valueOf() - which make it clearer (for a developer) you have to manipulate it, and perhaps reduce problems of showing the wrong timestamp to users. 在某些系统中,日期作为值存储在数据库中 - date.valueOf() - 这使得(对于开发人员)您必须更清楚地操作它,并可能减少向用户显示错误时间戳的问题。 In the other hand - you lose the readability. 另一方面 - 你失去了可读性。 Another opion is using only UTC time in your system and convert the timestamp on client side or before presenting the data. 另一种选择是在您的系统中仅使用 UTC 时间,并在客户端或呈现数据之前转换时间戳。 The use of UTC will make sure all of your dates will have same language '. 使用 UTC 将确保您的所有日期都具有相同的语言'.

If you want to read more about timestamp, here are some references:如果您想了解有关时间戳的更多信息,请参阅以下参考资料:

UTC vs ISO format for time UTC 与 ISO 时间格式

What is the "right" JSON date format? “正确”的 JSON 日期格式是什么?

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

相关问题 如何将 firebase 中的时间戳数据类型转换为日期? - How to convert timestamp data type in firebase to date? 如何使用 postgres(pg-promise)中的当前时间戳更新 null 时间戳? - How to update the null timestamp with current timestamp in postgres(pg-promise)? 如何从firebase实时数据库中检索数据使用时间戳? - How to retrieve data from firebase realtime database use timestamp? 如何从一个表中获取数据并在 sequelize(postgres) 中使用它 - How to get data from one table and use it in another in sequelize(postgres) 如何使用 knexjs 索引时间戳类型 - How to index timestamp type with knexjs 时间戳Postgres angularjs nodejs - Timestamp postgres angularjs nodejs 如何使用 where 子句查看 id 是否存在于 postgres 数组类型列中 - how to use a where clause to see if an id exists in a postgres array type column 在 NestJS 中,如何将数据类型为时间类型的字段统一转换为时间戳格式,然后返回给前端? - In NestJS, how do I uniformly convert a field whose data type is time type into a timestamp format and then return it to the front end? 你如何导入时间戳类型? - How do you import the timestamp type? Type(Java)Script,如何创建时间戳 - Type(Java)Script, how to create Timestamp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM