Piya
Piya

Reputation: 305

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=myDate.getFullYear();

      if(dateformat=="dd/mm/yyyy")         //checking the date format //
      {

        document.getElementById('date').value=day + "/" + month + "/" + yr;
      }
    else
     { 

     document.getElementById('date').value=month+"/"+day + "/" +yr;
     }
   }

This function onchange is written in onchange mehod of textbox date,on changing the textfield it should change the date format that is set by default.

If dd/mm/yyy is the format then change it in that format and if mm/dd/yyy then change in this format.My code does the changes, but it cannot recognize which is the month!

For example.. if the date format is mm/dd/yyy and I type the date as 11/01/2001' (NOV -1 2001) it changes to01/11/2011` which should not be done. But if I type 01-11-2001 (jan 1 2001) which is entered is correct ,but it changes to 11/01/2001

How can I change the code to correct this??? plz help!!!

Upvotes: 1

Views: 572

Answers (2)

Bhavik
Bhavik

Reputation: 4904

Demo Fiddle
Javascript Code

function dateChange() {
    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 = new Date(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 = new Date(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

Upvotes: 2

ankka
ankka

Reputation: 1

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.

Upvotes: 0

Related Questions