简体   繁体   English

JavaScript 将长日期转换为短日期

[英]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我有(德语)Output: Fri Sep 09 2022 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit) ,我想要yyyy-mm-dd日期: 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.日期在 JS 中并不太好,原因是像 moment.js 之类的库非常受欢迎。

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.但一个想法是简单地将 GMT+0300 替换为 GMT+0000,然后解析为 Date object 并使用 ToISOString 并对前 10 个字符进行切片。

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)'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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