add an extra day depending on time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • printline
    New Member
    • Jul 2006
    • 89

    #1

    add an extra day depending on time

    Hi' All

    I have an ordering formular where users can choose on have many days they want their order on. I use the below script to calculate and show the shipping date:

    Code:
    <script type="text/javascript">
    function setDelivery(val) {
    var delivery = new Date(); // We start on current day
    var theyear = delivery.getFullYear();
    var themonth = delivery.getMonth()+1;
    
    var i = 0; // i - Number of working days we have reached
    
    while (i < parseInt(val)) // We continue until we have reached the desired number of working days
    {
    	delivery.setDate(delivery.getDate() + 1); // Always jump one day ahead
    	if (delivery.getDay() != 0 && delivery.getDay() != 6) i++; // Provided that it is not a saturady or sunday we add a working day to i
    }
    
    // Now "delivery" should contain the desired date.
    
     //you have the correct date...
    document.getElementById("shopform").shipdate.value = delivery.getDate() + "-" + (delivery.getMonth() + 1) + "-" + delivery.getFullYear();
    }
    // end Helpers -->
    </script>
    What i want the script to do as an extra thing is to add one day extra, to the days already added, if the user uses the ordering formular after 3 p.m. Can this be done...???

    Example:

    A user orders at 4 p.m. on the 24th of November and wants the order on 2 days. So instead of delivery date being the 26th of November it should be the 27th of November...
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use getHours() to get the current hour. If it's greater than 15 (3pm), then add the extra day.

    Comment

    • printline
      New Member
      • Jul 2006
      • 89

      #3
      Hi'

      Tried doing what you suggested and my code looks like this now:

      Code:
      <script type="text/javascript"> 
      function setDelivery(val) { 
      var delivery = new Date(); // We start on current day 
      var theyear = delivery.getFullYear(); 
      var themonth = delivery.getMonth()+1;
      var thehours = delivery.getHours(); 
        
      var i = 0; // i - Number of working days we have reached 
        
      while (i < parseInt(val)) // We continue until we have reached the desired number of working days 
      { 
          delivery.setDate(delivery.getDate() + 1); // Always jump one day ahead 
          if (delivery.getDay() != 0 && delivery.getDay() != 6) i++; // Provided that it is not a saturady or sunday we add a working day to i 
          if (delivery.getHours() >= 15) i++;
      } 
        
      // Now "delivery" should contain the desired date. 
        
       //you have the correct date... 
      document.getElementById("shopform").shipdate.value = delivery.getDate() + "-" + (delivery.getMonth() + 1) + "-" + delivery.getFullYear(); 
      } 
      // end Helpers --> 
      </script>
      But this actually subtracts a day instead of adding a day.... Could you perhaps tell me what i'm doing wrong....????

      Thanks!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Rather than incrementing 'i', you need to set the date. Better still, work out the number of days separately and then just set the date at the end (outside the loop).

        Comment

        Working...