Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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

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.