Sequlize js / express js / node js how to change the date format

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:17:51

问题


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?


回答1:


I think you can use moment.js for this kind of work on client or on server




回答2:


You have selected a data type timestamp with timezone in database that's why it giving you time with timezone




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/52927251/sequlize-js-express-js-node-js-how-to-change-the-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!