Compare dates - iso format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maralfarshadi
    New Member
    • Jul 2007
    • 14

    #1

    Compare dates - iso format

    Hi everybody

    The date values entered in my text boxes (startDate and endDate) in my form are in ISO format YYYY-MM-DD.

    Could you please help me and explain how I can compare the date values to make sure the start date is smaller than the end date?

    I have something like the following, but it is not working (I guess due to ISO format)

    Code:
    if ( new Date(document.SearchFrm.startDate.value) > new Date(document.SearchFrm.endDate.value)){
    alert ("Start Date is greater");
    }

    Any tips is extremely appreciated,
    Maral
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Take the values and use split("-") to split the string into an array, then use setFullYear to set the date - see link.

    Comment

    • maralfarshadi
      New Member
      • Jul 2007
      • 14

      #3
      Originally posted by acoder
      Take the values and use split("-") to split the string into an array, then use setFullYear to set the date - see link.

      Thank you for your advice.
      Maral

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You're welcome. Let us know if you hit any problems. Good luck!

        Comment

        • maralfarshadi
          New Member
          • Jul 2007
          • 14

          #5
          Hi

          Appreciate your help and advice with the problem I have faced. My code is as follows:

          Code:
          function Compare() {
           
              var StartDateSt=document.SearchFrm.startDate.value;
              var EndDateSt=document.SearchFrm.endDate.value;
          
              StartDateSt = StartDateSt.split("-");
              EndDateSt = EndDateSt.split("-");
          
              var StartDate=new Date();
              StartDate.setFullYear(StartDateSt);
              
              var EndDate=new Date();
              EndDate.setFullYear(EndDateSt);
           
             alert(EndDate);
             alert(StartDate);
          
          }

          As the code is run, the two alert boxes return NaN.


          Trying to solve the problem in a different way, this time I let the user enter the date the usual way dd/mm/yyyy and use something like :

          Code:
              var StartDate = new Date(StartDateSt);
              var StartDateYear = StartDate.getYear();
              var StartDateMonth = StartDate.getMonth();
              var StartDateDay = StartDate.getDate();

          and then connect the different parts through "-". The problem I face here is that, the entered date in the textbox of my Web page is only processed correctly if it is entered as mm/dd/yyyy; Is this the default setting of dates in Javascript?

          How can I change the date format so the user can enter it as dd/mm/yyyy?


          Thank you in advance
          Maral

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Code:
            StartDate.setFullYear(StartDateSt);
            should be [CODE=javascript]StartDate.setFu llYear(StartDat eSt[0],StartDateSt[1],StartDateSt[2]);[/CODE]

            Comment

            Working...