Calculate difference bet two dates and set duration.

Tejasr1
Tera Contributor

Hi Team,

I have two fields( start date and end date ) with Date/Time Datatype and durationTime field with duration datatype.

Task:On change of end date ,calculate difference bet two dates and set duration with format days,hours,minutes,seconds. (ddd:HH:mm:ss)

I write script include with GlideAjax but it only calculate number of days.I want to calculate hours,minutes,seconds also.what changes I need to do in existing script?

Script Include:
var CalculateDate = Class.create();
CalculateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    calculateDatediff: function() {
        var startDate = this.getParameter('sysparm_start_date');
        var endDate = this.getParameter('sysparm_end_date');
        var gdt1 = new GlideDateTime(startDate);
        var gdt2 = new GlideDateTime(endDate);
        var dur = GlideDateTime.subtract(gdt1, gdt2);
        return dur.getDisplayValue();
        // }
    },
    type: 'CalculateDate'
});

On change Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('CalculateDate'); // CalculateDate is the script include name
    ga.addParam('sysparm_name', 'calculateDatediff'); // calculateDatediff is the function in the script include that we're calling
    ga.addParam('sysparm_start_date', g_form.getValue('u_startdate')); // get start date
    ga.addParam('sysparm_end_date', g_form.getValue('u_enddate')); // get end date
    ga.getXMLAnswer(DateParse);
}

// callback function for returning the result from the script include
function DateParse(response) {
    g_form.setValue('u_durationtime', response);

}

 

 

 

Thanks
Tejas

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Tejasr1 

Let's use the API below, it will return the difference between the two dates in the format ddd hh:mm:ss.

dateDiff(String startDate, String endDate, Boolean numericValue)

Returns
Type Description
StringIf the numericValue parameter is true, returns the difference between the two dates as an integer number of seconds; if false, returns the difference between the two dates in the format ddd hh:mm:ss.

 

Try the below adjustment.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('duration');
    }

	//Use this to convert from user to system format
	//If your instance use system format only, just ignore it
	var start_date = getDateFromFormat(g_form.getValue('u_startdate'), g_user_date_time_format);
	var format_start_date = formatDate(new Date(start_date), "yyyy-MM-dd hh:mm:ss");

	var end_date = getDateFromFormat(g_form.getValue('u_enddate'), g_user_date_time_format);
	var format_end_date = formatDate(new Date(end_date), "yyyy-MM-dd hh:mm:ss");

    var ga = new GlideAjax('CalculateDate');
    ga.addParam('sysparm_name', 'calculateDatediff');
    ga.addParam('sysparm_start_date', format_start_date);
    ga.addParam('sysparm_end_date', format_end_date);
    ga.getXMLAnswer(function(response) {
		g_form.setValue('u_durationtime', response);
    });
	
}
var CalculateDate = Class.create();
CalculateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    calculateDatediff: function() {
		var start = this.getParameter('sysparm_start_date');
		var end = this.getParameter('sysparm_end_date');
		var duration = gs.dateDiff(start, end);
		return duration;
    },
    type: 'CalculateDate'
});

 

 

 

Cheers,

Tai Vu

View solution in original post

5 REPLIES 5

Is there a way to factor in a schedule for the calculated duration? For example, Actual duration (work_duration) calculates a different value than Actual duration (actual_work_duration) OOB on planned task table for project and project task records due to the scheduled defined.