Add days to a date field

Forums Dynamic Forms for SharePoint Add days to a date field

Viewing 3 reply threads
  • Author
    Posts
    • #19571
      Eric Dickerson
      Participant

      Alex, I see this topic: https://spjsblog.com/forums/topic/rules-to-set-date-field-value-to-a-calculated-value/

      but I am hoping that you have some other way to add a number of days with a rule to a date field. Much like you have Today+7 in the set field value of a rule… I only need to take an existing date and add a number of days to it… {StartDate}+7.

      Any tips other than the JS in the topic above?

    • #19632
      Alexander Bautz
      Keymaster

      Hi,
      Unfortunately this is currently not possible in the set field value section. What you can do it to add the below snippet to your Custom JS and add a call to “setDateFromRule” in the “Run these functions / trigger these rules” section of the rule. I’ll add these functions to the spjs-utility.js file in the next version.

      function getDateFieldAsDateObject(fin){
          var dateVal, hour = 12, minute = 0, dateArr, lcid, format, fArr, sep, date, y, m , d;
          dateVal = jQuery("#dffs_" + fin).find("input:text").val();
          if(dateVal !== ""){
              if(jQuery("#dffs_" + fin).find("select").length > 0){
                  hour = jQuery("#dffs_" + fin).find("select:first").val();
                  minute = jQuery("#dffs_" + fin).find("select:last").val();
              }
              lcid = jQuery("#dffs_" + fin).find("a[onclick^='clickDatePicker']").attr("onclick").match(/lcid=([^&]*)/)[1];
              format = spjs.dffs.data.lcidToDateFormat[lcid];
              sep = format.match(/[^dmy]+/).toString();
              fArr = format.split(sep);
              dateArr = dateVal.split(sep);
              y = Number(dateArr[fArr.indexOf("y")]);
              m = Number(dateArr[fArr.indexOf("m")]);
              d = Number(dateArr[fArr.indexOf("d")]);
              date = new Date(y,m-1,d,hour,minute,0);
              return date;
          }else{
              return "";
          }
      }
      
      function setDateFieldFromDateObject(fin,date,offsetDays){
          if(date.getDate === undefined){
              return "";
          }
          var lcid, format, sep, dStr, hour, minute;
          lcid = jQuery("#dffs_" + fin).find("a[onclick^='clickDatePicker']").attr("onclick").match(/lcid=([^&]*)/)[1];
          format = spjs.dffs.data.lcidToDateFormat[lcid];
          dStr = format;
          sep = format.match(/[^dmy]+/).toString();
          if(typeof offsetDays === "number"){
              date.setDate(date.getDate() + offsetDays);
          }
          dStr = dStr.replace("m", (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1));
          dStr = dStr.replace("d", date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
          dStr = dStr.replace("y", date.getFullYear());
          jQuery("#dffs_" + fin).find("input:text").val(dStr);
          hour = date.getHours();
          minute = date.getMinutes() - date.getMinutes() % 5;
          if(minute < 10){
              minute = "0"+minute;
          }
          jQuery("#dffs_" + fin).find("select:first").val(hour);
          jQuery("#dffs_" + fin).find("select:last").val(minute);
      }
      
      function setDateFromRule(){
          setDateFieldFromDateObject("DateColumn2",getDateFieldAsDateObject("DateColumn1"),7);
      }

      Change “DateColumn1” and “DateColumn2” to match your fields.

      Let me know how this works out.

      Alexander

    • #22490
      MikeS
      Participant

      The Custom JS above works great with a very similar requirement we had: enter a single date and have 13 other date fields change to 13 different dates when form is saved.

      Thanks Alexander.

    • #31803
      Taylor Murphy
      Participant

      Hi Alex!

      I was able to get the following code to work in my SharePoint, which is awesome! However, I am looking to modify this to have the date populate to the next business day. For example, I have a rule set up to calculate the next Follow Up Date (field 1) based on the Initial Entry Date (field 2) and the value selected in the Follow Up Type field (field 3). Right now, this code works perfectly, except for the fact that it will set the Follow Up Date to a weekend. I still need it to add calendar days (do not want it to add business days) but want to have it set to populate the next business day if it happens to fall on a Sat or Sun.
      Example:
      {Initial Entry Date}+3
      {Thu,10/1/2020} + 3 = Mon,10/5/2020 (instead of Sun, 10/4/2020}

      Any help is greatly appreciated!!!

      • #31811
        Alexander Bautz
        Keymaster

        Try something like this:

        var theDate = new  Date(); // use the date you get from the function instead of "new Date();"
        var weekday = theDate.getDay();
        if(weekday === 6){ // Saturday
            theDate.setDate(theDate.getDate() + 2);
        }
        // Sunday
        if(weekday === 0){
            theDate.setDate(theDate.getDate() + 1);
        }
        console.log(theDate);

        Alexander

      • #31826
        Taylor Murphy
        Participant

        Thanks so much for your response! Where would I put that code? Would it replace a specific part of the code referenced previously? I am having a hard time figuring out where exactly to place the code within the snippet from 02/2018.

      • #31834
        Alexander Bautz
        Keymaster

        You are not supposed to modify the existing code – you must run this code after you have first used the existing code to get your date object back – then use this date as X in the “var theDate = X”.

        Alexander

Viewing 3 reply threads
  • You must be logged in to reply to this topic.