Skip to content Skip to sidebar Skip to footer

Change The Date Format My Identifying The Date And Month?

function date() { var myDate=new Date(document.getElementById('date').value); //get the date from a textfield var day=myDate.getDate(); var month=myDate.getMonth()+1; var yr

Solution 1:

Demo FiddleJavascript Code

functiondateChange() {
    var e = document.getElementById("dateformat");
    var dateformat = e.options[e.selectedIndex].text;
    var myDate;
    if (dateformat == "dd/mm/yyyy") //checking the date format //
    {
        var value = document.getElementById('date').value;
        var format = value.split("/");
        myDate = newDate(format[2], format[1] - 1, format[0]);
        var day = myDate.getDate();
        var month = myDate.getMonth() + 1;
        var yr = myDate.getFullYear();
        document.getElementById('date').value = day + "/" + month + "/" + yr;
    } else {
        myDate = newDate(document.getElementById('date').value);
        var day = myDate.getDate();
        var month = myDate.getMonth() + 1;
        var yr = myDate.getFullYear();
        document.getElementById('date').value = month + "/" + day + "/" + yr;
    }
    document.getElementById('dateStr').innerHTML = myDate.toDateString();
}  

Enter the date 01/02/2014 with mm/dd/yyyy in drop down the date would be Thu Jan 02 2014, now just change the drop down to dd/mm/yyyy the date would be Sat Feb 01 2014

Instantiating Javascript's Date object require this format new Date(yyyy,mm,dd), so when you use dd/mm/yyyy you need to manually ex-change the dd & mm values... Reference

Solution 2:

I guess the problem is that you either don't have dateformat specified or you compare it wrong.

if(dateformat=="dd/mm/yyyy")

If that always returns false, you will always get the second option.

Make sure dateformat is visible in the date() function, and make sure it actually gets the value you want it to have.

Secondly - the new Date() function actually parses the date before you check for the date format. I think you might want to do it the other way around: parse the input date, check which format it is in, and then output the result.

Post a Comment for "Change The Date Format My Identifying The Date And Month?"