You are currently viewing ServiceNow GlideDate – Very Helpful!

ServiceNow GlideDate – Very Helpful!

ServiceNow GlideDate class provides methods for performing operations on GlideDate objects,
such as instantiating GlideDate objects or working with GlideDate fields. In this post, I have identified a few most common ServiceNow examples and tried to provide solutions in the form of scripts.

How to get the date in a specified format in ServiceNow GlideDate?

We often need to change the format of a date object into any other form, mostly in scripts such as business rules, workflow, etc. To overcome this problem ServiceNow GlideDate class provides a function called getByFormat(String format).

// Change format of date
var gd = new GlideDate(); 
gd.setValue('2021-04-21');
gs.print(gd.getByFormat("dd-MM-yyyy"));
// Output
*** Script: 21-04-2021

//Extract day name from date
var gd = new GlideDate(); 
gd.setValue('2021-04-21');
gs.print(gd.getByFormat("EEEE"));
// Output
*** Script: Wednesday

Use the below Letters to get various information from the date:-

SimpleDateFormat

Get the day of the month from the ServiceNow GlideDate object?

In order to retrieve the day of the month stored by the GlideDate object, expressed in the UTC time
zone, we use getDayOfMonthNoTZ() function.

//Today's date is 2022-09-19
var gd =new GlideDate();
gs.info(gd.getDayOfMonthNoTZ())
//Output
*** Script: 19

Get date value in the current user’s display format?

ServiceNow GlideDate class provides function getDisplayValue() to retrieve date values according to user’s format. It changes when the user changes the date format in the user profile.

// Display Date
var gd =new GlideDate();
gs.info(gd.getDisplayValue());
//Output
*** Script: 19/09/2022

How to get the date value in the current user’s display timezone?

To get the value of the date according to the logged in users timezone, use getDisplayValueInternal() function. It overrides the user’s date format and prints the date as per timezone.

// Current timezone - Eastern
var gd = new GlideDate(); 
gs.info(gd.getDisplayValueInternal());
//Output
*** Script: 2022-09-19

How to get the value of a month from the GlideDate object?

To retrieve the value of the month use function – getMonthNoTZ().

//Today's date is 2022-09-19
var gd =new GlideDate();
gs.info(gd.getMonthNoTZ());
//Output
*** Script: 9

How to get the value of the year from the current date?

//Today's date is 2022-09-19
var gd =new GlideDate();
gs.info(gd.getYearNoTZ());
//Output
*** Script: 2022

This Post Has One Comment

  1. Marc Lindsay

    How can you add a number of days to a GlideDate (or any date) in a Scope Application ?

    Taking today’s date and need to add a number of days, to find the new date.

Leave a Reply