Reputation: 2804
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.
Upvotes: 0
Views: 615
Reputation: 53198
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>
Upvotes: 2
Reputation: 1529
Convert to moment object then format it out in desired format.
moment('1-20-2017', 'M-DD-YYYY').format('YYYY-MM-DD')
Upvotes: 0