How to change date format using jQuery?

User1988 picture User1988 · Jan 6, 2014 · Viewed 141.5k times · Source

I have a date in a format like this fecha2.value = '2014-01-06', but I want to change the format to this '01-06-14' using jQuery.

How can I do this? Thanks in advance.

Answer

Rory McCrossan picture Rory McCrossan · Jan 6, 2014

You can use date.js to achieve this:

var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');

Alternatively, you can do it natively like this:

var dateAr = '2014-01-06'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);

console.log(newDate);