简体   繁体   中英

JavaScript Date/Time Formatting

I am needing to convert the standard Date(); JS object to the following format: 7/13/2021 8:47:58 PM (M/d/yyyy HH:mm:ss Z)

I'm struggling to get this EXACT format in the simplest way possible. This is what I have so far:

 var d = new Date, dformat = [d.getMonth()+1, d.getDate(), d.getFullYear()].join('/')+' '+ [d.getHours(), d.getMinutes(), d.getSeconds()].join(':'); console.log(dformat);

I am struggling to get the time zone, which I am conjecturing adds the AM/PM (I could very well be mistaken).

Any help is greatly appreciated!

 var d = new Date, dformat = [d.getMonth()+1, d.getDate(), d.getFullYear()].join('/')+' '+ d.toLocaleTimeString('en-US') console.log(dformat);

Try this:

 var d = new Date(); console.log(d.toLocaleString());

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