The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Business Rule to calculate duration not work

Jason Nicholas
Tera Expert

Hi, I have a business rule where I am trying to calculate the following on a project:

1. Project During between start and end dates

2. Remaining Duration between now and project end date

 

Example start date 01/09/2025 00:00:00 & End Date 31/10/2025 00:00:00

The Project Duration is returning a figure of  (60 Days 16 Hours)

The Remain Duration figure changes every time I save but not in the correct way, its random (63 Days 23 Hours 36 Minutes then 63 Days 3 Hours 1 Minute then 63 Days 33 Minutes then 63 Days 22 Hours 49 Minutes)

 

Obviously my calculations are wrong somewhere, can anyone assist and point out where

 

 

(function executeRule(current, previous /*null when async*/) {

/**********************************************************/
/* Get todays date & start and end dates from the project */
/**********************************************************/
        var todaysDate = new GlideDate();
        var todaysDateSec = todaysDate.getNumericValue();

        var startDate = new GlideDateTime(current.start_date);
        var startDateSec = startDate.getNumericValue();

        var endDate = new GlideDateTime(current.end_date);
        var endDateSec = endDate.getNumericValue();

/******************************************************************************************************************/
/* Calculate difference in seconds between start and end dates & also difference between todays date and end date */
/******************************************************************************************************************/    
        var dateDiff = (endDateSec - startDateSec);
        var dateRemain = (endDateSec - todaysDateSec);

/****************************************************/
/* Set up the variables for Days, Minutes and Hours */
/****************************************************/
        var secondsInAMinute = 60.0;
        var secondsInAnHour = 60.0 * secondsInAMinute;
        var secondsInADay = 24.0 * secondsInAnHour;

/****************************************************************************/
/* Calculate Date Difference between Planned Start and End dates of Project */
/****************************************************************************/
        var durationSeconds = parseInt(dateDiff, 10);

    // Extract Days and round up to whole number
        var durDays = Math.floor(durationSeconds / secondsInADay / 1000);

    // extract hours
        var durHourSeconds = durationSeconds % secondsInADay;
        var durHours = Math.floor(durHourSeconds / secondsInAnHour);

    // extract minutes
        var durMinuteSeconds = durHourSeconds % secondsInAnHour;
        var durMinutes = Math.floor(durMinuteSeconds / secondsInAMinute);

    // extract the remaining seconds
        var durRemainSeconds = durMinuteSeconds % secondsInAMinute;
        var durSeconds = Math.ceil(durRemainSeconds);

    // Convert days to string to add to duration field
        var duration = (durDays.toString() + ' ' + durHours.toString() + ":" + durMinutes.toString() + ":" + durSeconds.toString());

/**********************************************************************************/
/* Calculate Date Difference between Planned End dates of Project and todays Date */
/**********************************************************************************/
    //var rem_time= dateRemain;

        var remainSeconds = parseInt(dateRemain, 10);

    // Extract Days and round up to whole number
        var remDays = Math.floor(remainSeconds / secondsInADay / 1000);

    // extract hours
        var remHourSeconds = remainSeconds % secondsInADay;
        var remHours = Math.floor(remHourSeconds / secondsInAnHour);

    // extract minutes
        var remMinuteSeconds = remHourSeconds % secondsInAnHour;
        var remMinutes = Math.floor(remMinuteSeconds / secondsInAMinute);

    // extract the remaining seconds
        var remRemainingSeconds = remMinuteSeconds % secondsInAMinute;
        var remSeconds = Math.ceil(remRemainingSeconds);
    // Convert days to string to add to duration field
        var remain = (remDays.toString() + ' ' + remHours.toString() + ":" + remMinutes.toString() + ":" + remSeconds.toString());

        current.u_project_duration = duration;

        if (dateRemain > 0){
            current.u_days_remaining = remain;
        }
        if (dateRemain <= 0){
            remain = ('0'.toString());
            current.u_days_remaining = remain;
        }
           
})(current, previous);

 

1 ACCEPTED SOLUTION

@Jason Nicholas 

Glad to know that my script worked.

I believe I shared the working code and you can enhance it further based on your developer skills and experience.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

@Jason Nicholas 

Glad to know that my script worked.

I believe I shared the working code and you can enhance it further based on your developer skills and experience.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Bert_c1
Kilo Patron

I use the following logic:

 

	// Calculating a duration field based on two date-time values
	var testRec = new GlideRecord('u_test_table');
	testRec.query();
	while (testRec.next()) {
		var	dateDiff = testRec.u_end_date.dateNumericValue() - testRec.u_start_date.dateNumericValue();
		gs.info('Start dateTime: ' + testRec.u_start_date + ', End dateTime: ' + testRec.u_end_date);

		var numSecsDays = 24 * 60 * 60;
		numSecsHour = 60 * 60;
		numSecsMinute = 60;
		var numSecs = dateDiff / 1000;
		var numDays = Math.floor(numSecs / numSecsDays);
		var remSecs = numSecs - (numDays * numSecsDays);
		var numHours = Math.floor(remSecs / numSecsHour);
		remSecs -= (numHours * numSecsHour);
		var numMinutes = Math.floor(remSecs /numSecsMinute);
		remSecs -= (numMinutes * numSecsMinute);
		gs.info('Days: ' + numDays + ', hours: ' + numHours + ', minutes: ' + numMinutes + ', seconds: ' + remSecs);
	}

the results:

*** Script: Start dateTime: 2025-08-21 16:11:14, End dateTime: 2025-08-24 15:14:14
*** Script: Days: 2, hours: 23, minutes: 3, seconds: 0
*** Script: Start dateTime: 2025-05-24 20:57:58, End dateTime: 2025-05-26 21:58:59
*** Script: Days: 2, hours: 1, minutes: 1, seconds: 1

Hi 

Ankur's script worked but I will also try yours to see if it gives me better results where I have the 1 issue

Thanks