Add days to a date field

Forums Dynamic Forms for SharePoint Add days to a date field

Tagged: 

This topic contains 2 replies, has 3 voices, and was last updated by  Mike 2 days, 10 hours ago.

  • Author
    Posts
  • #19571

    Eric Dickerson
    Participant

    Alex, I see this topic: http://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

    Mike
    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.

You must be logged in to reply to this topic.