The CreatorCon Call for Content is officially open! Get started here.

how to calculate date/ time difference of two variables?

Puneet4418
Tera Contributor

Hi All,

 

could you please help me with the following requirement. we have 2 date/ time variables and a single line text variable. we want to calculate the date/ time difference from the two variables and paste the result in single line text filed. 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Puneet4418 

it should be simple one.

write 2 onChange catalog client script i.e. 1 each on each date/time variable and use GlideAjax and get the difference and store in single line text variable

something like this

onChange client script

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

    var startDate = g_form.getValue('start_date_variable');
    var endDate = g_form.getValue('end_date_variable');

    if (startDate && endDate) {
        var ga = new GlideAjax('DateDifferenceCalculator');
        ga.addParam('sysparm_name', 'calculateDifference');
        ga.addParam('startDate', startDate);
        ga.addParam('endDate', endDate);
        ga.getXMLAnswer(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            g_form.setValue('single_line_text_variable', answer);
        });
    }
}

Script Include: It should be client callable

var DateDifferenceCalculator = Class.create();
DateDifferenceCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    calculateDifference: function(startDate, endDate) {
        var start = new GlideDateTime(this.getParameter('sysparm_startDate'));
        var end = new GlideDateTime(this.getParameter('sysparm_endDate'));
        var duration = GlideDateTime.subtract(end, start);
        return duration.getDisplayValue();
    },

    type: 'DateDifferenceCalculator'
});

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

4 REPLIES 4

Runjay Patel
Giga Sage

christopher_cox
ServiceNow Employee
ServiceNow Employee

Try something like this 

 

var startDate = new GlideDateTime(your_variable);

var endDate = new GlideDateTime(your_second_variable);
var diff = gs.dateDiff(startDate.getDisplayValue(),endDate.getDisplayValue(),true); //returns value in seconds

your_string_variable.setDateNumericValue(diff*1000); 

 

Please mark helpful

Ankur Bawiskar
Tera Patron
Tera Patron

@Puneet4418 

it should be simple one.

write 2 onChange catalog client script i.e. 1 each on each date/time variable and use GlideAjax and get the difference and store in single line text variable

something like this

onChange client script

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

    var startDate = g_form.getValue('start_date_variable');
    var endDate = g_form.getValue('end_date_variable');

    if (startDate && endDate) {
        var ga = new GlideAjax('DateDifferenceCalculator');
        ga.addParam('sysparm_name', 'calculateDifference');
        ga.addParam('startDate', startDate);
        ga.addParam('endDate', endDate);
        ga.getXMLAnswer(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            g_form.setValue('single_line_text_variable', answer);
        });
    }
}

Script Include: It should be client callable

var DateDifferenceCalculator = Class.create();
DateDifferenceCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    calculateDifference: function(startDate, endDate) {
        var start = new GlideDateTime(this.getParameter('sysparm_startDate'));
        var end = new GlideDateTime(this.getParameter('sysparm_endDate'));
        var duration = GlideDateTime.subtract(end, start);
        return duration.getDisplayValue();
    },

    type: 'DateDifferenceCalculator'
});

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

@Puneet4418 

Hope you are doing good.

Did my reply answer your question?

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