Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to get difference between two date/time type of fields and populate it in a field which is of duration type

akshay parekar1
Tera Contributor

Hello Guys,please help me to achieve this,

For my  custom table, i have created two date/time type of fields which are Start Date and End Date.

I have created  third field also named as Duration  which is of duration type

Now, whenever value of End Date field changes on the form, my duration field should populate the difference between Start Date and End Date in duration form( days and difference in time)

To achieve this i have written a script include and called it from onchange type of client script(Field name= End Date), 

still my Duration field is not getting populated as i desired

please see my script and help me out, Thanks in advance!!

 

Script Include:

var DurationCalculation = Class.create();
DurationCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDuration: function() {
var dateTimediff;
    var date1= new GlideDateTime();
    var date2= new GlideDateTime();
    
    var fromdate= this.getParameter('sysparm_from_date');
    var todate= this.getParameter('sysparm_to_date');
    
    date1.setValue(fromdate);
    date2.setValue(todate);
    
    dateTimediff=GlideDateTime.substract(date1,date2);
    return dateTimediff.getDisplayvalue();
},
    type: 'DurationCalculation'
});

 

Client script:(Type- onChange and field name-End Date)

 

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

    var fromdate = g_form.getValue('u_start_date');
    var todate = g_form.getValue('u_end_date');
    
    var ga= new GlideAjax('DurationCalculation');
    ga.addParam('sysparm_name','getDuration');
    ga.addParam('sysparm_from_date',fromdate);
    ga.addParam('sysparm_to_date',todate);
    ga.getXML(CBF);
    
    function CBF(response){
        var answer= response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_duration',answer);
        
    }


}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var DurationCalculation = Class.create();
DurationCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDuration: function() {
        var dateTimediff;
        var date1= new GlideDateTime();
        var date2= new GlideDateTime();

        var fromdate= this.getParameter('sysparm_from_date');
        var todate= this.getParameter('sysparm_to_date');

        date1.setValue(fromdate);
        date2.setValue(todate);

        dateTimediff = GlideDateTime.substract(date1,date2);
        return dateTimediff.getDurationValue();
    },
    type: 'DurationCalculation'
});

Regards
Ankur

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

View solution in original post

10 REPLIES 10

Harish KM
Kilo Patron
Kilo Patron

Hi can you try the below

  var fromdate = this.getParameter('sysparm_from_date');
        var todate= this.getParameter('sysparm_to_date');
        
        var from_date = new GlideDateTime();
        from_date.setDisplayValue(fromdate); // Set the due date display value
        
        var end_date = new GlideDateTime();
        end_date.setDisplayValue(todate); // Set the due date display value
       

        var today = new GlideDateTime().getDisplayValue();
  gs.info("today" + today);

       
 var duration = GlideDateTime.subtract(from_date,end_date, );
        gs.info("duration" + duration);
        var days = Number(duration.getDisplayValue().toString().split(" ")[0]);
         gs.info("days" + days);
       return days;

 

Refer

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0824807

Regards
Harish

I don't have to make any changes to my client script,

i just have to replace your script include with mine, right?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var DurationCalculation = Class.create();
DurationCalculation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDuration: function() {
        var dateTimediff;
        var date1= new GlideDateTime();
        var date2= new GlideDateTime();

        var fromdate= this.getParameter('sysparm_from_date');
        var todate= this.getParameter('sysparm_to_date');

        date1.setValue(fromdate);
        date2.setValue(todate);

        dateTimediff = GlideDateTime.substract(date1,date2);
        return dateTimediff.getDurationValue();
    },
    type: 'DurationCalculation'
});

Regards
Ankur

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

Thanks for this,

can you please reply me how to achieve above requirement by after business rule,

i don't need to write script include over there, right?