Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript

This should do the magic

var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");

You can use the following to convert DD-MM-YYYY to YYYY-MM-DD format using JavaScript:

var date = "24/09/2018";
date = date.split("/").reverse().join("/");

var date2 = "24-09-2018";
date2 = date.split("-").reverse().join("-");

console.log(date); //print "2018/09/24"
console.log(date2); //print "2018-09-24"

Don't use the Date constructor to parse strings, it's extremely unreliable. If you just want to reformat a DD-MM-YYYY string to YYYY-MM-DD then just do that:

function reformatDateString(s) {
  var b = s.split(/\D/);
  return b.reverse().join('-');
}

console.log(reformatDateString('25-12-2014')); // 2014-12-25

Tags:

Javascript