简体   繁体   中英

Standardize date format using Moment.js

From 1-20-2017 (a string), I want to format a date as yyyy-mm-dd , like 2017-01-20 .

I've tried to dig into the Moment.js documentation , but couldn't find any method that does this. I don't want an ISO date, I just want it to remain as string.

You can use format() in combination with moment() :

 var newFormat = moment("1-20-2017", "M-DD-YYYY").format('YYYY-MM-DD'); document.getElementById('output').innerHTML = newFormat; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script> <div id="output"></div> 

转换为矩对象,然后将其格式化为所需格式。

moment('1-20-2017', 'M-DD-YYYY').format('YYYY-MM-DD')

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