Usage of the .subtract() method

atishaytoma
Tera Contributor

What is the .subtract() method used for. 

 

7 REPLIES 7

Ravi Gaurav
Giga Sage
Giga Sage

Hi @atishaytoma 
It used as below

subtract(GlideDateTime start, GlideDateTime end)

Returns the duration difference between two specified GlideDateTime objects.

Parameters
Name Type Description
start GlideDateTime Start date object.
end GlideDateTime End date object.
Returns
Type Description
GlideDuration Duration difference between the two specified dates.

Scoped equivalent:

To use the subtract() method in a scoped application, use the corresponding scoped method: subtract().

Example

 

 

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

Output

 

2 Days 23 Hours
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

SeemaJenitT
Tera Contributor

How we can subtract the date, weeks, months, year from the current date?

see:

 

GlideDateTimeAPI subtract

 

for usages of 'subtract()'.