简体   繁体   English

序列化js / express js / node js如何更改日期格式

[英]Sequlize js / express js / node js how to change the date format

When I see the dates stored in my database. 当我看到存储在数据库中的日期时。

在此处输入图片说明

This is normal . 很正常 The date and the time that's all. 日期和时间就这样。

But when I run a get request to get the data. 但是,当我运行get请求以获取数据时。

在此处输入图片说明

THIS FORMAT IS NOT SAME AS THE FORMAT THAT STORED IN THE DATABASE . 此格式与数据库中存储的格式不同 Why could this happen? 为什么会发生这种情况?

我认为您可以在客户端或服务器上使用moment.js进行此类工作

您已经在数据库中选择了带有时区的数据类型时间戳,这就是为什么它会给您带时区的时间的原因

THIS FORMAT IS NOT SAME AS THE FORMAT THAT STORED IN THE DATABASE . 此格式与数据库中存储的格式不同 Why could this happen? 为什么会发生这种情况?

Answer to this is : 答案是:

When you fetch data from DB , Sequlize js will convert it to the UTC time , and DB UI will show you as your local time zone. 当您从DB中获取数据时,Sequlize js会将其转换为UTC time,并且DB UI将显示您的本地时区。 So there is nothing to worry about it as its storing and giving the 100% correct date. 因此,不必担心它的存储和给出100%正确的日期。

To make it sure : 为了确保:

You can take the date and time from DB and convert it to UTC , and then you can check that one with result returned by Sequlize js. 您可以从DB中获取日期和时间,并将其转换为UTC,然后可以检查Sequlize js返回的结果是否为该日期和时间。

To change the Date format you can use moment.js , I think this can clear all your doubts 要更改Date格式,您可以使用moment.js ,我认为这可以消除您的所有疑问

We need not install any package for these kind of things as we can handle the date formats using MySQL itself From your pic I can see you are using MySQL 我们不需要为此类事情安装任何软件包 ,因为我们可以使用MySQL本身处理日期格式。 从您的图片中可以看到您正在使用MySQL

Just use dateformat from MySQL 只需使用MySQL中的dateformat

Select id, date_format(createdAt, '%d-%m-%Y %r') from table_name;

Note: %r for time with AM or PM 注意:%r表示有AM或PM的时间

To dive in deep: https://www.w3schools.com/sql/func_mysql_date_format.asp 深入了解: https : //www.w3schools.com/sql/func_mysql_date_format.asp

For further assistance you can comment down 要获得更多帮助,您可以评论一下

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

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