Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DD-MM-YYYY format to MM-DD-YYYY in moment.js

I am trying to convert a date from DD-MM-YYYY to MM-DD-YYYY. But it doesn't work for me. I tried this

var test = Date.parse('15-06-2010');
var SearchDate = moment('15-06-2010').format("MM-DD-YYYY");

var day = moment(new Date("15-06-2010"), "MM-DD-YYYY");

console.log(test);
console.log(day);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

Help me.

like image 917
Nagarjuna Reddy Avatar asked Oct 07 '16 12:10

Nagarjuna Reddy


People also ask

How can I get date in dd mm yyyy format in moment?

SearchDate = moment(new Date(), “DD/MM/YYYY”);

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy:Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

What is Moment () in JavaScript?

Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.


2 Answers

Try this:

var date = moment('15-06-2010', 'DD-MM-YYYY')
console.log(date.format('MM-DD-YYYY'))

You have to specify the input format for the date when parsing a date with momentjs.

like image 159
nagyf Avatar answered Oct 04 '22 17:10

nagyf


Here 26-11-2019 will be converted to 11-26-2019

const date =  moment('26-11-2019','DD-MM-YYYY').format('MM-DD-YYYY');

To the format method we can give any format, how you want the output.

like image 28
Nayan Patel Avatar answered Oct 04 '22 17:10

Nayan Patel