简体   繁体   English

使用Moment.js标准化日期格式

[英]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 . 1-20-2017 (字符串),我想将日期格式设置为yyyy-mm-dd ,例如2017-01-20

I've tried to dig into the Moment.js documentation , but couldn't find any method that does this. 我试图深入研究Moment.js文档 ,但是找不到任何执行此操作的方法。 I don't want an ISO date, I just want it to remain as string. 我不想要ISO日期,我只希望它保留为字符串。

You can use format() in combination with moment() : 您可以将format()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')

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

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