You are currently viewing Common ServiceNow Date Queries

Common ServiceNow Date Queries

Dear Spectaculars,

Welcome to the new article. This article is dedicated to all basic queries related to ServiceNow dates. In this article, I have tried to provide well-tested scripts along with output that can help you develop. Please provide your valuable feedback in the comments and any other queries.

How do I add milliseconds to the GlideDateTime Object in ServiceNow?

// Adding 10 miliseconds to the sample date

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gs.print(gdate.getNumericValue());
gdate.add(10);
gs.print(gdate.getNumericValue());

// Output
*** Script: 1314777600000
*** Script: 1314777600010

How to add hours to the GlideDateTime Object in ServiceNow?

// Adding 1 hour to the sample date
// 1 hour = 60 minutes = 60 × 60 seconds = 3600 seconds = 3600 × 1000 milliseconds = 3,600,000 ms

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gs.print("Sample Date - "+gdate);
gdate.add(3600000);
gs.print("Updated Date - "+gdate);

// Output
*** Script: Sample Date - 2011-08-31 08:00:00
*** Script: Updated Date - 2011-08-31 09:00:00

How to add minutes to the GlideDateTime Object in ServiceNow?

// Adding 30 minutes to the sample date
// 30 min = 1800000 ms.
var gdate = new GlideDateTime("2011-08-31 08:00:00");
gs.print("Sample Date - "+gdate);
gdate.add(1800000);
gs.print("Updated Date - "+gdate);

// Output
*** Script: Sample Date - 2011-08-31 08:00:00
*** Script: Updated Date - 2011-08-31 08:30:00

How to add days to current GlideDateTime object in ServiceNow?

/* Adding 2 Days to sample date */

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gdate .addDays(2);
gs.print(gdate);
// Output
*** Script: 2011-09-02 08:00:00

How to subtract days from current GlideDateTime object in ServiceNow?

/* subtracting 2 Days to sample date */

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gdate .addDays(-2);
gs.print(gdate);
// Output
*** Script: 2011-08-29 08:00:00

How to add days as per the Local timing of the User in ServiceNow?

/* Adding 2 Days to sample date */

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gdate .addDaysLocalTime(2);
gs.print(gdate.getLocalDate());
// Output
*** Script: 2011-09-02

How to subs days as per the Local timing of the User in ServiceNow?

/* subtracting 2 Days to sample date */

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gdate .addDaysLocalTime(-2);
gs.print(gdate.getLocalDate());
// Output
*** Script: 2011-08-29

How to use the addDaysUTC() function in ServiceNow?

/* Adding 2 Days to sample date */

var gdate = new GlideDateTime("2011-08-31 08:00:00");
gdate .addDaysUTC(2);
gs.print(gdate.getLocalDate());

// Output
*** Script: 2011-09-02

How to add seconds to the current time in ServiceNow?

/* Adding 120 Secs to current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addSeconds(120);
gs.print(gdate.getValue());

// Output
*** Script: 2022-08-16 02:04:16
*** Script: 2022-08-16 02:06:16

How to add Weeks to current date in ServiceNow?

/* Adding 2 Weeks to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addWeeks(2);
gs.print(gdate);

// Output
*** Script: 2022-08-16 02:08:44
*** Script: 2022-08-30 02:08:44

How to subtract Weeks from current date in ServiceNow?

/* Subtracting 2 Weeks to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addWeeks(-2);
gs.print(gdate);

// Output
*** Script: 2022-08-16 02:11:23
*** Script: 2022-08-02 02:11:23

How to add months to current date in ServiceNow?

/* Adding 2 Months to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addMonths(2);
gs.print(gdate);

// Output
*** Script: 2022-08-16 02:18:43
*** Script: 2022-10-16 02:18:43

How to subs months from the current date in ServiceNow?

/* Subs 2 Months to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addMonths(-2);
gs.print(gdate);
// Output
*** Script: 2022-08-16 02:19:57
*** Script: 2022-06-16 02:19:57

Use addMonthsLocalTime to update local months

#Pro-Tip

How to add years to current date in ServiceNow?

/* Add 2 Years to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addYears(2);
gs.print(gdate);

// Output
*** Script: 2022-08-16 02:37:39
*** Script: 2024-08-16 02:37:39

How to subs years to current date in ServiceNow?

/* Subs 2 Years to the current date */

var gdate = new GlideDateTime(gs.nowDateTime());
gs.print(gdate);
gdate.addYears(-2);
gs.print(gdate);

// Output
*** Script: 2022-08-16 02:49:45
*** Script: 2020-08-16 02:49:45

Leave a Reply