Calc dates moving unchanged date field

I have a function to take the first day of leave requested and the last day and calculate the number of business days that equals in total.

The function is called whenever the is a change to either the first date field or the last date field. The function runs perfectly the first time I run it. It also runs perfectly every time I change the first date. But after it does the first calculation when I try and edit the second date it starts moving the first date value being reported to java even though nothing is changing onscreen. If I comment out the calculator function altogether and just console log, the 2 dates when I change the end date reports the dates as expected. But this is what it looks like in sequence with the calc on…

First Day of Absence: 9/21/2020 / Last Day of Absence: 9/23/2020
console.log:
First Day = Mon Sep 21 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time), Last Day = Wed Sep 23 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time)

First Day of Absence: 9/21/2020 / Last Day of Absence: 9/24/2020
console.log:
First Day = Thu Sep 24 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time), Last Day = Thu Sep 24 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time)

First Day of Absence: 9/21/2020 / Last Day of Absence: 9/23/2020
console.log:
First Day = Fri Sep 25 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time), Last Day = Wed Sep 23 2020 00:00:00 GMT+1000 (Australian Eastern Standard Time)

These are the 2 calls that tell the function to run:
fd.field('FirstDay').$on("change",function(value) {
calcDays(value, fd.field('LastDay').value);
});
fd.field('LastDay').$on("change",function(value) {
console.log("First Day = " + fd.field('FirstDay').value + ", Last Day = " + value);
calcDays(fd.field('FirstDay').value, value);
});

And this is the function itself:
function calcDays(startDate, endDate) {
//This calculates suggested days of Leave by taking first day and last day and using 5-day work week.
var count = 0;
var curDate = startDate;
while (curDate <= endDate) {
var dayOfWeek = curDate.getDay();
if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
count++;
curDate.setDate(curDate.getDate() + 1);
}
fd.field('TotalDays').value = count;
};
};

I have even tried creating global variables for the startDate and endDate and setting them when I call the $on("change" instead of passing the values through as arguments / parameters but it still seems to modify the fd.field('FirstDate').value to the date of the last calculation in the while loop. Interestingly enough, if I save the form in this state it saves the start date that's shown on screen and not the one the is being reported in js.

Hello @tmoreno,

Welcome to Plumsail Community!

You can calculate the number of business days between days using this code:

requirejs.config({
    paths: {
        moment: "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min",
        'moment-business-days': "https://cdn.jsdelivr.net/npm/moment-business-days@1.1.3/index.min"
    }
});

fd.spRendered(function() {

    require(['moment'], function(moment) {
        require(['moment-business-days'], function() {

            function calcDiff() {
                var startDate = moment(fd.field('FirstDay').value);
                var endDate = moment(fd.field('LastDay').value);
                var diff = endDate.businessDiff(startDate);
                fd.field('TotalDays').value = diff;
            }

            function defineWorkDays () {
                moment.updateLocale('us', {
                    // Defines days from 1 (Monday) to 6 (Saturday) as business days. Note that Sunday is day 0.
                    // When omitting this configuration parameter, business days are based on locale default
                    workingWeekdays: [1, 2, 3, 4, 5]
                });
            }

            // Defining Work Days on form loading
            defineWorkDays ();

            // Calling function when the user changes the date
            fd.field('FirstDay').$on('change', calcDiff);
            fd.field('LastDay').$on('change', calcDiff);

            // Calling function on form loading
            calcDiff();
        });
    });
});

Please find more information on how to work with Date and Time fields in our documentation here.

Let us know if you have any questions.

Thanks for this! It's all working now!

1 Like