Skip to content
Advertisement

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

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"
    }

Advertisement

Answer

This should do the magic

var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement