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 subtract two date and time fields in server script

Kishor O
Tera Sage

How to subtract two date and time fields in server script

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Here is an example

 

var gdt1 = new GlideDateTime("2011-08-31 08:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");
var dur = GlideDateTime.subtract(gdt1, gdt2);
gs.info(dur.getDisplayValue());

if you have a field you want to use you can pass it as an argument like below

var gdt1 = new GlideDateTime(current.<field name>);

 

-Anurag

SN_Learn
Kilo Patron
Kilo Patron

Hi @Kishor O ,

 

var gdt1 = new GlideDateTime("2024-05-28 09:00:00");
var gdt2 = new GlideDateTime("2024-05-31 18:00:00");
 
var dur = GlideDateTime.subtract(gdt1, gdt2); // Difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());

 

ServiceNow Document for more details: GlideDateTime - Global 

 

Please consider marking my reply as Helpful and/or Accept Solution, if it helps. Thanks!

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.