How to convert date format from dd-MMM-yyyy to yyyymmdd in Javascript?

kumareloaded picture kumareloaded · Dec 9, 2014 · Viewed 14.8k times · Source

I've an input text box with a date-picker, when the user selects a particular date, the date say 09-Dec-2014 gets applied to the input text box. On submit, I want the date to be passed to the server in yyyymmdd format say 20141209. So how to convert the date format in javascript?

var date = new Date(Date.parse('09-Dec-2014'));

Above code gives me 'Invalid date'.

Searched the net for solution but was not able to find for my problem.

Can someone help me out?

Thanks.

Answer

Danyal Sandeelo picture Danyal Sandeelo · Dec 9, 2014
 var date = '09-Dec-2014'.split("-");
 var newDate=date[2]+date[1]+date[0];

Something really quick may be, split and generate your own.