1 Reply Latest reply on Aug 27, 2015 6:13 PM by tma@actionlife.com

    Business Days Date Calculation

    tma@actionlife.com

      Good Afternoon,

       

      I have read dozens of posts on this topic, but I am still struggling to make this work. Can any of you help me?

       

      I have three fields: dropdown, date1, and date2.

      • dropdown has four options for turnaround time: Five Day, Three Day, Next Day, and Same Day
      • date1 automatically populates today's date using this custom calculation script: event.target.value = util.printd("m/d/yy", new Date());
      • I need date2 to populate (date1 + X days) based on the dropdown selection. I would like a cut-off time of 1:00pm, such that a "Same Day" dropdown selection at 11:00am would result in a date2 date of today, but a "Same Day" dropdown selection at 3:00pm would result in a date2 date of tomorrow.


      I am trying to use something like the code below as custom calculation scripts in date2, but I can't quite get this to work properly:

       

      if (this.getField(dropdown).value == "Five Day")

      {

      (function () {

      var sDate = this.getField("date1").valueAsString;

      var t = util.scand("m/d/yy", sDate);

      var days = 5;

      t.setDate(t.getDate() + days);

      var nAdj = 0;

      var nDay = t.getDay();

      if(nDay == 0) nAdj = 1;

      if(nDay == 6) nAdj = 2;

      t.setDate(t.getDate() + nAdj); 

      if (sDate) {event.value = util.printd("m/d/yy", t);

      } else {

      event.value = "";

      }

      })();

      }

       

      And I would repeat these lines for the other dropdown options, changing "Five Day" to "Three Day" and "5" to "3" for example.

      I'm not sure how to incorporate the 1PM cut-off time either... Maybe something with a t.getHour line?

      I also can't figure out how to skip holidays..

       

      Can any of you help??

      Thanks so much,

      Tim

        • 1. Re: Business Days Date Calculation
          tma@actionlife.com Level 1

          Right now, I put this code below into the validation section of the dropdown. It is working pretty well, but sometimes it skips weekends and other times it doesn't. I can't exactly figure out what's happening on the weekend portion. And I still don't know how to implement the 1PM cut-off time. Any thoughts?

           


          if(event.value == "Five Day Standard")

          {

          (function () {

          var sDate = this.getField("date1").valueAsString;

          var t = util.scand("m/d/yy", sDate);

          var days1 = 5;

          t.setDate(t.getDate() + days1);

          var nAdj = 0;

          var nDay = t.getDay();

          if(nDay == 0) nAdj = 1;

          if(nDay == 6) nAdj = 2;

          t.setDate(t.getDate() + nAdj); 

          if (sDate) {this.getField("date2").value = util.printd("m/d/yy", t);

          } else {

          this.getField("date2").value = "";

          }

          })();

          }

           

           

          if(event.value == "Three Day Rush")

          {

          (function () {

          var sDate = this.getField("date1").valueAsString;

          var t = util.scand("m/d/yy", sDate);

          var days2 = 3;

          t.setDate(t.getDate() + days2);

          var nAdj = 0;

          var nDay = t.getDay();

          if(nDay == 0) nAdj = 1;

          if(nDay == 6) nAdj = 2;

          t.setDate(t.getDate() + nAdj); 

          if (sDate) {this.getField("date2").value = util.printd("m/d/yy", t);

          } else {

          this.getField("date2").value = "";

          }

          })();

          }

           

           

          if(event.value == "Next Day Rush")

          {

          (function () {

          var sDate = this.getField("date1").valueAsString;

          var t = util.scand("m/d/yy", sDate);

          var days3 = 1;

          t.setDate(t.getDate() + days3);

          var nAdj = 0;

          var nDay = t.getDay();

          if(nDay == 0) nAdj = 1;

          if(nDay == 6) nAdj = 2;

          t.setDate(t.getDate() + nAdj); 

          if (sDate) {this.getField("date2").value = util.printd("m/d/yy", t);

          } else {

          this.getField("date2").value = "";

          }

          })();

          }

           

           

           

          if(event.value == "Same Day Rush")

          {

          (function () {

          var sDate = this.getField("date1").valueAsString;

          var t = util.scand("m/d/yy", sDate);

          var days4 = 0;

          t.setDate(t.getDate() + days4);

          var nAdj = 0;

          var nDay = t.getDay();

          if(nDay == 0) nAdj = 1;

          if(nDay == 6) nAdj = 2;

          t.setDate(t.getDate() + nAdj); 

          if (sDate) {this.getField("date2").value = util.printd("m/d/yy", t);

          } else {

          this.getField("date2").value = "";

          }

          })();

          }