简体   繁体   中英

JavaScript converting long date to short date

I have the (german) Output: Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit) and I would like to have just the yyyy-mm-dd date: 2022-09-09

Thank you for the help!

Date's are not too great in JS, the reason that libs like moment.js etc are very popular.

But one idea is to simply replace the GMT+0300 to GMT+0000, and then parse into Date object and use ToISOString and slice the first 10 chars.

eg.

 function shortDate(dt) { return new Date(dt.replace(/GMT\+..../,'GMT+0000')).toISOString().slice(0, 10); } console.log(shortDate('Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)'));

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