Date calculation not quite functioning correct.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plumba
    New Member
    • Aug 2007
    • 57

    Date calculation not quite functioning correct.

    Hiya all.

    I have a form which the user selects a date from a javascript style calendar, it puts the date into a field in the format dd/mm/yyyy.

    I want the form to compare this user selected date to a date which it will work out, which is today's date pluys 7 days.

    The reason is, the field is a 'Required by' field on a form and I dont want the user to enter a date in the past or one which is less than 7 days from today.

    So it will comare the date entered to a date 7 days from today.

    I have the logic right (I think), and included it here, but for somereason a date where the day is 10th or greater seems to trips up!

    Any ideas???


    Code:
    <script>
    	function checkadate(){
    
    //This bit gets todays date and add seven days
    var currentdate = new Date();
    currentdate.setDate(currentdate.getDate()+7);
    var day = currentdate.getDate();
    var month = currentdate.getMonth() + 1;
    var year = currentdate.getFullYear();
    
    //This bit puts that date in to dd/mm/yyyy format
    var todat = (day + "/" + month + "/" + year)
    
    alert("Todays date plus 7 is " +todat);
    
    //This is the date entered by the user.
    var ReqDate =  document.getElementById("DateRequired").value;
    
    alert("Selected date is " + ReqDate);
    
    var SDate = (todat);
    var EDate =  document.getElementById("DateRequired").value;
    var endDate = new Date(EDate);
    var startDate= new Date(SDate);
    
    
    if(startDate > endDate){
    	alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
    	document.getElementById("DateRequired").value = SDate;
    	return false;
    	}else{
    }
    
    
    
    }
    </script>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You already have currentdate as a Date object for the start date. Create another date object using the user inputted date via the setFullYear method, then compare the two.

    Comment

    • plumba
      New Member
      • Aug 2007
      • 57

      #3
      Pardon my ignorance, but I'm not quite following...... . :/

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        On lines 5-6, you created a date object and set the date.

        One line 17, you have the date value for the end date. Split the string using str.split("/") to get an array of date/month/year values. Then create a new Date object and use setFullYear() to set the date.

        Now you have two date objects ready for comparing.

        Comment

        • plumba
          New Member
          • Aug 2007
          • 57

          #5
          Again, I can see the logic, but there must be a problem with my date formatting, I'm now getting NaN on the third alert box - when trying to setFullYear on ReqDate.

          I've modified and simplified the script, it was over kill last time, but it's still not working it out. Does the setDate getDate put it into dd/mm/yyyy or american format?
          The value I'm bringing in from the form is in dd/mm/yyyy, would that cause a problem?

          I've included updated code....

          Thanks in anticipation...

          Code:
          <script>
          	function checkadate(){
          
          //Gets todays date and add seven days.
          var currentdate = new Date();
          currentdate.setDate(currentdate.getDate()+7);
          alert(currentdate);
          
          
          //Gets date entered
          var ReqDate =  document.getElementById("DateRequired").value;
          
          //Splits date into an array
          var ReqArray=ReqDate.split("/");
          alert(ReqArray);
          
          var ReqDate = new Date();
          ReqDate.setFullYear(ReqArray);
          alert(ReqDate);
          //**Failing here**
          
          
          if(ReqDate > currentdate){
          	alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
          	document.getElementById("DateRequired").value = SDate;
          	return false;}
          
          }
          </script>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use the array values separately and see how setFullYear is also used (with three parameters).
            [CODE=javascript]//Gets date entered
            var ReqDate = document.getEle mentById("DateR equired").value ;

            //Splits date into an array
            var ReqArray=ReqDat e.split("/");

            var ReqDate = new Date();
            ReqDate.setFull Year(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
            [/CODE]

            Comment

            • plumba
              New Member
              • Aug 2007
              • 57

              #7
              Brilliant!! Got it working, many thanks for your xcellent help as usual!

              I've posted the working code for anyone else to use in the future, as it took me AGES to get working what on reflection is a simple task.

              Jut change the params as you see fit.

              <
              Code:
              script>
              function comparetwodates(){
              //Gets todays date and add seven days.
              var currentdate = new Date();
              currentdate.setDate(currentdate.getDate()+7);
              
              //Gets date entered
              var myDate =  document.getElementById("mydate").value;
              
              //Splits date into three components
              var ReqArray=myDate.split("/"); //Or change the '/' for your date seperator
              
              //Pieces date back as a date
              var myDate = new Date();
              ReqDate.setFullYear(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
              
              
              //Checks to see if myDate is earlier than currentdate
              if(ReqDate < currentdate){
              
              	alert("My Date is less than 7 days from today.");
                              return false;
              }
              }
              </script>

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Yes, you just need to know how dates work in JavaScript. You may find this link useful (if you haven't already seen it) and the accompanying reference.

                Anyway, glad to hear that you've got it working.

                Comment

                Working...