Don't Get Time-Warphed! Using GlideDate and GlideDateTime In ServiceNow
Tags: javascript, api, cloud, development
Published: 2024-04-17 18:52:11

**`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdpwydxq9dwcicw875yb.png)

-`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01dv1pmfwbih4mv4rrxp.png)

- `getDayOfMonthNoTZ()`: Retrieves the day of the month, expressed in the UTC time zone.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8833eyfdw148ctabqdj.png) 

###########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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/40hqhkhmd3thcv12n0u1.png)

- `getDisplayValueInternal()`: Retrieves the date value in the internal format (yyyy-MM-dd) and time zone regardless of user preferences.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3snxpdupl4qmbv9htrhx.png)

- `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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx9hoz46l8uoqxq33ef8.png)

- `getNumericValue()`: Retrieves the date value as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxbuqlev33s1gnmnp94y.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1hb4gjaz2szhdih44r4.png)

####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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0uqd95x1b1qxom0bvtz.png)

-`subtract()`: Subtracts one GlideDate object from another, returning the difference in days.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weist4ujqf48osy9xstr.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf0cwjoik7oxyrj99v10.png)

`getDayOfWeek()`: Returns the day of the week for the GlideDateTime object expressed in the users time zone.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19nf6pml311oq5ve1oaf.png)

`getYear()`: Returns the year for the GlideDateTime object.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f76r2kndq17ttvgxam77.png)

`getDisplayValue()`: Returns the date and time in the display format according to the user's preferences.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9bu601rgzpim5auj21p.png)

`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb1uorzdsgejzguwpxyc.png)

##########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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjyamc4h6xdb9abi87ns.png)


`subtract()`: Subtracts specified amount time.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08a30w764u8z58il4aw7.png)


####<u>Choosing the Right Tool for the Job</u>
**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3dfl26zw561ccazt9jb.png)

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