Jenny Mok
Jenny Mok

Reputation: 2804

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.

Upvotes: 0

Views: 615

Answers (2)

BenM
BenM

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

dmoo
dmoo

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

Related Questions