简体   繁体   中英

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.

在此处输入图片说明

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. So there is nothing to worry about it as its storing and giving the 100% correct date.

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.

To change the Date format you can use moment.js , I think this can clear all your doubts

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

Just use dateformat from MySQL

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

Note: %r for time with AM or PM

To dive in deep: https://www.w3schools.com/sql/func_mysql_date_format.asp

For further assistance you can comment down

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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