user7035864
user7035864

Reputation:

How to format date using momentjs in javascript

I'm using moment.js in my project, what I want is to convert a date in format dd-mm-yyyy to yyyy-mm-dd. I tried this but it didn't work, it gives me an invalid date:

alert(moment('14/10/2017').format('YYYY/MM/DD'));

Upvotes: 0

Views: 39

Answers (1)

Djaouad
Djaouad

Reputation: 22766

You need to specify the current format of your date in moment():

var date = moment('14/10/2017', 'DD/MM/YYYY').format('YYYY/MM/DD');
console.log(date); // 2017/10/14
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>

Upvotes: 2

Related Questions