0

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'));

1 Answer 1

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks please how can get only HH:MM from Sat Sep 23 2017 07:04:08 GMT+0100 ??
@imsiimsi var date = moment('Sat Sep 23 2017 07:04:08 GMT+0100').format('hh:mm');.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.