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 the time difference between two date/time fields in number of Days

Kaleem A
Giga Guru

Hi All,

I have below script. How to get the time difference between two date/time fields in number of Days. If the difference is   less than a day the value of dur is "HH:MM:SS".   It creates confusion with the parseInt() function. I'm looking for the dur value in "DD HH:MM:SS". Can some one help me with this?

var actualDateTime = new GlideDateTime();

actualDateTime.setDisplayValue(gs.nowDateTime());

var gr = new GlideRecord('change_request');

gr.addQuery('number','CNG0286539');

gr.query();

if(gr.next()){

  aging = "";

              var impendTime = gr.end_date.getDisplayValue();//implementationEndTime

              gs.print(impendTime);

              var dur = gs.dateDiff(impendTime, actualDateTime);

              var d = dur.getDay();

              gs.print(actualDateTime);

              gs.print (dur);

              var d=parseInt(dur);

              gs.print(d);

}

1 ACCEPTED SOLUTION

Kaleem A
Giga Guru

This script helped me



var actualDateTime = new GlideDateTime();


actualDateTime.setDisplayValue(gs.nowDateTime());


var gr = new GlideRecord('change_request');


gr.addQuery('number','CNG0286538');


gr.query();


if(gr.next()){


              var impendTime = gr.end_date.getDisplayValue();//implementationEndTime


                gs.print(actualDateTime);


              dur = gs.dateDiff(impendTime, actualDateTime);


              var dateDifference = new GlideDuration(dur);


              gs.print ("dateDifference : " + dateDifference);


              var d   = dateDifference.getDayPart();


              gs.print("d :" + d);


           


       


             


}


View solution in original post

3 REPLIES 3

Karthik Reddy T
Kilo Sage

Hello Akbar,

 

 

 

Refer the below similar threads may helpful to you.

 

 

 

Find difference between two dates

 

 

 

Compare two date fields & calculate days between the two?

 

 

 

Comparing dates

 

 

 

How to get difference between two date/time stamp in number of days and excluding saturday sundays ?

 

Karthik Reddy T.
ServiceNow Commnunity MVP -2018 class.

Thanks Karthik,



I have referred these threads. These doesn't help much


Kaleem A
Giga Guru

This script helped me



var actualDateTime = new GlideDateTime();


actualDateTime.setDisplayValue(gs.nowDateTime());


var gr = new GlideRecord('change_request');


gr.addQuery('number','CNG0286538');


gr.query();


if(gr.next()){


              var impendTime = gr.end_date.getDisplayValue();//implementationEndTime


                gs.print(actualDateTime);


              dur = gs.dateDiff(impendTime, actualDateTime);


              var dateDifference = new GlideDuration(dur);


              gs.print ("dateDifference : " + dateDifference);


              var d   = dateDifference.getDayPart();


              gs.print("d :" + d);


           


       


             


}