简体   繁体   English

如何将日期字符串转换为日期使用 javascript moment.js

[英]how to convert date string to date use javascript moment.js

how to convert date string: example: with format "dd M yyyy" => "07 Okt 2020" wont to date "2020-10-07"如何转换日期字符串:示例:格式为“dd M yyyy”=>“07 Okt 2020”不会日期为“2020-10-07”

We can parse after setting the moment locale to German, this includes a tweak to exclude the period from short month names.我们可以在将 moment locale 设置为德语后进行解析,这包括一个从短月份名称中排除句点的调整。

For example:例如:

 // We must set the months up to exlude the trailing period "." so we parse our date correctly. moment.updateLocale('de',{ monthsShort: ['Jan', 'Febr', 'Mrz', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sept', 'Okt', 'Nov', 'Dez'] }); let dateStr = "07 Okt 2020"; let dt = moment(dateStr, "DD MMM YYYY", "de"); console.log("Date formatted as YYYY-MM-DD:", dt.format("YYYY-MM-DD"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment-with-locales.min.js" integrity="sha512-EATaemfsDRVs6gs1pHbvhc6+rKFGv8+w4Wnxk4LmkC0fzdVoyWb+Xtexfrszd1YuUMBEhucNuorkf8LpFBhj6w==" crossorigin="anonymous"></script>

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

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