www.webdeveloper.com
Results 1 to 2 of 2

Thread: compare dates in javascript

  1. #1
    Join Date
    Jul 2013
    Posts
    6

    Question compare dates in javascript

    I am trying to validate date based on date entered in first textbox. If second textbox exceeds one year from the date entered in first textbox, then it should display an alert and blank the second date field textbox. Both the textboxes are readonly and gets the values from calender. I tried the below code and it is not validating if second textbox value is greater than one year. Any help !

    PHP Code:
      function test()
      {
          
    start document.getElementById('name3').value;
          
    end document.getElementById('name4').value;
          
    compare(startend);
          
      }

      function 
    compare(sDateeDate)
      {
          
          function 
    parseDate(input) {
                var 
    parts input.match(/(\d+)/g);
                 
                return new 
    Date(parts[2], parts[0]-1parts[1]); // months are 0-based. Parts2 contains year, parts0 contains month and parts1 contains date.
              
    }
           var 
    parse_sDate parseDate(sDate);
          var 
    parse_eDate parseDate(eDate);
          
    parse_sDate.setDate(parse_sDate.setFullYear(parse_sDate.getMonth() + 12));
          if(
    parse_sDate>parse_eDate)
              {
              
    alert("End date should not be greater than one year from start date");
              }
           
      }
      
    </script>
    </head>
    <body>
    <form>
    <table>
    <tr>
    <td><input type="text" name="soname3" id="name3" size="15" readonly="readonly"> 
    <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
     <td><input type="text" name="soname4" id="name4" size="15" readonly="readonly">
     <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12'); " /> </td>
     </tr>
    </table>
    <input type="submit" value="Submit" onclick="test()">
    </form> 

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,140
    Sounds like a homework assignment.

    A pointer is to calculate a year as milliseconds.

    Add that to the initial time, convert the second date to milliseconds and then if second date <= then its within the year.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles