Understanding GlideDate() and GlideDateTime() in ServiceNow:

Selva Arun
Mega Sage
Mega Sage

Understanding GlideDate() and GlideDateTime() in ServiceNow:

ServiceNow developers frequently work with date and time data to manage records and processes effectively. GlideDate() and GlideDateTime() are essential classes in ServiceNow for handling date and time operations programmatically. This article provides a detailed overview of these classes, their methods, practical examples, and the importance of initialization.

What are GlideDate() and GlideDateTime()?

GlideDate(): GlideDate is a ServiceNow class used for managing and manipulating dates without time components. It enables developers to perform operations such as date parsing, formatting, and comparisons.

GlideDateTime(): GlideDateTime extends GlideDate to include both date and time components. It offers additional functionalities such as time zone management, date-time arithmetic, and formatting date-time strings.

Initialization of GlideDate() and GlideDateTime() Objects

When initializing GlideDate or GlideDateTime objects (var gd = new GlideDate(); or var gdt = new GlideDateTime();), you capture the current system date and time. This ensures that your scripts operate within the current system context, preventing errors in date and time calculations and respecting time zone configurations.

Basic Usage and Examples

  1. Creating a GlideDate Object

To create a GlideDate object representing the current date:

var gd = new GlideDate();

  1. Creating a GlideDateTime Object

To create a GlideDateTime object representing the current date and time:

var gdt = new GlideDateTime();

 

 

Methods and Operations

Getting and Setting Date/Time

GlideDate Methods:

  • getDisplayValue(): Returns the date in 'YYYY-MM-DD' format suitable for display purposes.

Example:

var gd = new GlideDate();

var displayDate = gd.getDisplayValue();

gs.info('Current Date: ' + displayDate); // Outputs: Current Date: 2024-06-21

  • addDays(int): Adds a specified number of days to the current date.

Example:

var gd = new GlideDate();

gd.addDays(5); // Adds 5 days to the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-06-26

  • addWeeks(int), addMonths(int), addYears(int): Adds a specified number of weeks, months, or years to the current date.

Examples:

var gd = new GlideDate();

gd.addWeeks(2); // Adds 2 weeks to the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-07-05

 

gd.addMonths(3); // Adds 3 months to the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-09-21

 

gd.addYears(1); // Adds 1 year to the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2025-06-21

 

  • subtract(int), subtractWeeks(int), subtractMonths(int), subtractYears(int): Subtracts a specified number of days, weeks, months, or years from the current date.

Examples:

var gd = new GlideDate();

gd.subtract(5); // Subtracts 5 days from the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-06-16

 

gd.subtractWeeks(2); // Subtracts 2 weeks from the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-06-07

 

gd.subtractMonths(3); // Subtracts 3 months from the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2024-03-21

 

gd.subtractYears(1); // Subtracts 1 year from the current date

gs.info('Updated Date: ' + gd.getDisplayValue()); // Outputs: Updated Date: 2023-06-21

 

GlideDateTime Methods:

  • getDayOfWeek(): Returns the day of the week as an integer (1-7).

Example:

var gdt = new GlideDateTime();

var dayOfWeek = gdt.getDayOfWeek();

gs.info('Day of Week: ' + dayOfWeek); // Outputs: Day of Week: 5

  • getLocalDate(), getLocalTime(): Retrieves the date or time portion based on the local time zone.

Examples:

var gdt = new GlideDateTime();

var localDate = gdt.getLocalDate();

gs.info('Local Date: ' + localDate); // Outputs: Local Date: 2024-06-21

 

var localTime = gdt.getLocalTime();

gs.info('Local Time: ' + localTime); // Outputs: Local Time: 14:30:00

  • getDayOfMonth(): Returns the day of the month as an integer (1-31).

Example:

var gdt = new GlideDateTime();

var dayOfMonth = gdt.getDayOfMonth();

gs.info('Day of Month: ' + dayOfMonth); // Outputs: Day of Month: 21

  • getMonthOfYear(): Returns the month of the year as an integer (1-12).

Example:

var gdt = new GlideDateTime();

var monthOfYear = gdt.getMonthOfYear();

gs.info('Month of Year: ' + monthOfYear); // Outputs: Month of Year: 6

  • getYear(): Retrieves the year portion of the date.

Example:

var gdt = new GlideDateTime();

var year = gdt.getYear();

gs.info('Year: ' + year); // Outputs: Year: 2024

 

Date-Time Arithmetic and Formatting

Formatting Date-Time Strings

  • To format a GlideDateTime object according to the user’s preferred format:

var gd = new GlideDate();

gd.setValue('2021-04-21');

gs.info(gd.getByFormat("dd-MM-yyyy"));//output: 21-04-2021

Handling Incoming Excel Date Formats

  • Excel often stores dates as numeric values. To convert an incoming Excel date to a GlideDate or GlideDateTime object:

// Assuming the Excel date value is '20210420'

var excelDateValue = '20210420';

 

// Convert to 'yyyy-MM-dd' format

var formattedExcelDateValue = excelDateValue.substring(0, 4) + '-' +

                              excelDateValue.substring(4, 6) + '-' +

                              excelDateValue.substring(6, 8);

 

// Convert to GlideDate

var gdFromExcel = new GlideDate();

gdFromExcel.setValue(formattedExcelDateValue);

gs.info('GlideDate from Excel: ' + gdFromExcel.getDisplayValue());

 

// Convert to GlideDateTime

var gdtFromExcel = new GlideDateTime();

gdtFromExcel.setValue(formattedExcelDateValue);

gs.info('GlideDateTime from Excel: ' + gdtFromExcel.getDisplayValue());

 

Conclusion

Mastering GlideDate and GlideDateTime is essential for efficiently managing date and time data in ServiceNow applications. By understanding these classes and their methods, developers can perform accurate date operations, handle time zone conversions, and format date-time strings according to user preferences.

Whether you’re a ServiceNow developer exploring date functionalities or looking to enhance your skills in managing temporal data, GlideDate and GlideDateTime offer powerful capabilities to streamline application development and improve user experience.

For more advanced operations and customization, ServiceNow’s comprehensive documentation and community forums are invaluable resources.

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideDate...

 

If you believe this document is helpful, could you please **mark it as 'Helpful'**? This will help other community members who might have the same question find the answer more easily.

 

Please feel free to correct this document if it is wrong!

 

Thank you for your consideration.

 

Selva

 

Selva

1 REPLY 1

Amy Hicox
Tera Contributor

How can I convert to/from epoch time format?

also what date/time format does GlideRecord require when setting a dateTime field value?

sorry if this is the wrong place to ask!