简体   繁体   中英

Convert date and time format from PHP to JavaScript

I have a problem that in my MySQL, the date and time data is saved as Y-mm-dd H:m:s . How can I switch to the date and time to JavaScript?

Your date is saved as date in sql.

When you have a backend, an ajax request from browser can get a date or bigger structure as json (serialized as string). Sample: { date: "2020-05-23T16:29:48.973Z" }. .

This date can be (should be) in an iso format, see ISO 8601 . This date string can be parsed by javascript easily:

let dateObj = new Date("2020-05-23T16:29:48.973Z");

This object can be formatted (object -> string) with help of Intl.DateTimeFormat .

let str = new Intl.DateTimeFormat('en-US').format(dateObj);
console.info('date: ', str);

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