I'm trying to convert date format (DD-MM-YYYY) to (YYYY-MM-DD).i use this javascript code.it's doesn't work.
function calbill()
{
var edate=document.getElementById("edate").value; //03-11-2014
var myDate = new Date(edate);
console.log(myDate);
var d = myDate.getDate();
var m = myDate.getMonth();
m += 1;
var y = myDate.getFullYear();
var newdate=(y+ "-" + m + "-" + d);
alert (""+newdate); //It's display "NaN-NaN-NaN"
}
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:
You just need to use return newdate:
demo
But I would simply recommend you to use like @Ehsan answered for you.
You can use the following to convert DD-MM-YYYY to YYYY-MM-DD format using JavaScript:
This should do the magic