DEV Community

Cover image for Don't Get Time-Warphed! Using GlideDate and GlideDateTime In ServiceNow
Sophia Semga
Sophia Semga

Posted on

Don't Get Time-Warphed! Using GlideDate and GlideDateTime In ServiceNow

What day is it today?? Can someone Glidey check the time and let us know.πŸ˜…

We spent the past few weeks exploring ServiceNow JavaScript APIs and today I glidely present to you two essential API tools for managing dates and times:

GlideDate and GlideDateTime.

While both reside within the ServiceNow platform and function as server-side APIs, they cater to different scenarios and offer unique sets of functionalities.

GlideDate shines as an essential component within the platform that serves as a server side API class used for managing and manipulating date values.
It is primarily used when dealing with dates exclusively and represents only the day component without any time information. Think birthdays, anniversaries, deadlines, or any scenario where only the day component matters. GlideDate effortlessly manages these dates, storing them in the familiar "yyyy-MM-dd" format.
It also offers a comprehensive set of methods for working with dates and a fundamental tool for developers building custom functionalities involving calculation of date comparisons, and formatting.
Developers can access and initiate GlideDate object using the GlideDate() function in server side scripts, to create a new GlideDate object representing the current date. This object can be manipulated using various methods provided by the GlideDate API.

Some methods include:

  • getDate(): Retrieves the date portion of the GlideDate object in the form "yyyy-MM-dd". Image description

-getByFormat(): Retrieves the date portion of the GlideDate object in the specified format "dd-MM-yyyy" or β€œEEEE” to get day of the week.
Image description

  • getDayOfMonthNoTZ(): Retrieves the day of the month, expressed in the UTC time zone. Image description
#####P.S, GlideDate Objects are stored in UTC (Coordinated Universal Time) by default. This is crucial to remember when displaying dates to users across different time zones.
####If you need to adjust for time zones, consider leveraging the GlideTimeZone class (not covered in this blog, but readily available in ServiceNow documentation).
  • getDisplayValue(): Retrieves the date value and time zone formatted based on the user's display preferences.
    Image description

  • getDisplayValueInternal(): Retrieves the date value in the internal format (yyyy-MM-dd) and time zone regardless of user preferences.
    Image description

  • setValue(String value): Sets the value of the GlideDate object. The input must be in the internal format "yyyy-MM-dd" and the system time zone by default.
    Image description

  • getNumericValue(): Retrieves the date value as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
    Image description

-isInPast(), isInFuture(): Checks if the GlideDate is in the past or future, respectively.

Image description

Similarly, you can manipulate dates by adding/subtracting days, weeks, etc using method like:

  • addDays(Number days), addWeeks(Number weeks), addMonths(Number months): Adds the specified number of days, weeks, or months to the GlideDate object. Image description

-subtract(): Subtracts one GlideDate object from another, returning the difference in days.
Image description

GlideDate object can also be used to compare dates using operators like > (greater than), < (less than), and == (equal to), making decision making in workflows a breeze .

Next,

We have our timekeeper API also known as GlideDateTime.
GlideDateTime is another service side API that represents date and time combined. Think timestamps, time zone conversions, managing time-bound SLAs (Service Level Agreements), tracking login events with precise timestamps, or scheduling tasks with specific deadlines. GlideDateTime effortlessly handles all of these scenarios, representing dates and times in the format "yyyy-MM-dd HH:mm:ss [year, month, day, hour, minute, second]”.

To invoke the GlideDateTime method, similarly to GlideDate use new GlideDateTime() or provide a specific date and time string for more control.
GlideDateTime inherits all functionalities of GlideDate for date manipulation, comparison, and formatting, but some of it’s additional methods for time handling include:

getDayOfMonth(): Returns the current day of the month for the GlideDateTime object in UTC time zone.
Image description

getDayOfWeek(): Returns the day of the week for the GlideDateTime object expressed in the users time zone.
Image description

getYear(): Returns the year for the GlideDateTime object.
Image description

getDisplayValue(): Returns the date and time in the display format according to the user's preferences.
Image description

getDisplayValueInternal(): Returns the date and time in internal format, yyyy-MM-dd HH:mm:ss. This method is useful for date/time fields, but not for date fields.
Image description

####It also includes additional methods for time handling, such as adding/subtracting hours, minutes, seconds, extracting extra time components like getHours(), getMinutes(), and getSeconds()
####and time conversions(with GlideTimeZone)

getUserTimeZone(): Retrieves the time zone for the current user session.
Image description

subtract(): Subtracts specified amount time.
Image description

Choosing the Right Tool for the Job

Date Only? GlideDate is your champion for scenarios where time is irrelevant (e.g., birthdays, anniversaries).
Dates and Times? GlideDateTime reigns supreme when you need to manage both dates and times (e.g., deadlines, scheduled events). GlideDateTime's versatility makes it a future-proof choice, as you can always incorporate time elements if needed.

Remember: GlideDateTime builds upon GlideDate, offering all its functionalities in addition to time management. This makes it the more versatile option for most development scenarios.

Image description

Stay tuned for more insights on JavaScript APIs within the ServiceNow platform. We’ve merely scratched the surface, and there’s so much more knowledge waiting for us to explore.

Until our next β€œglide” (or should I say β€œdive”) into the depths of JavaScript APIs,

####Happy coding! πŸ˜„

.Learn More About ServiceNow GlideDate: https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_reference/GlideDate/concept/GlideDateAPI.html

.Learn More About ServiceNow GlideDateTime:
https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html

Top comments (0)