Use Moment - Js in ServiceNow - John Skender
Use Moment - Js in ServiceNow - John Skender
Use Moment - Js in ServiceNow - John Skender
When GlideDateTime methods don’t quite cut it, we can rely on the world’s most popular
library for parsing, validating, and manipulating dates and time in javascript: Moment.js
It’s fairly quick to get started using moment.js in both client and server side code. We’ll
walk through the process of getting it installed and briefly show how you can create a
moment object from a GlideDateTime field and vice versa.
1. Client Installation
2. Server Installation
3. Basic Usage
UI scripts will make the library available in the client (UI policies, UI actions, Client Scripts,
etc) and Script Includes will make it available in Server code (Other Script Includes,
Business Rules, Fix Scripts, etc).
Client Side
Navigate to https://momentjs.com/downloads/moment.min.js and copy the entirety of the
minified library.
Create a new UI Script in your instance and paste the contents of the script into the script
field. You’ll need to set the following properties on the script
Global = true
UI Type = Desktop
Active = true
Name = moment
You should now have access to the moment object anywhere client side throughout the
platform.
Server Side
Navigate to https://raw.githubusercontent.com/jnskender/Moment-
ServiceNow/master/moment.min.js and copy the entirety of the minified library.
Delete all auto generated code from the new script include.
You should see similar output to below if the script include was successfully configured.
2020-08-29T10:57:07-07:00
Basic Usage
To show you how you can get started using moment in relation to ServiceNow, we’ll go
through a brief example of creating a moment instance from a GlideDateTime field,
modify the instance using some common moment functions, and update that field with
the modified time.
First we need to grab a record from the system that has at least one column of type
Date/Time. Change Request has one out of the box called Planned start date(start_date).
Lets grab a change that has that value populated.
In this case we’re just printing out the number to make sure we have a record that meets
that condition. If you don’t, please create a change and fill out that field to meet the above
conditions.
The moment library is smart enough to understand the format of GlideDateTimes display
value so we don’t need to explicitly set it.
Printing the value startDate.format() should return the moment formatted string:
2020-10-20T07:30:00-07:00
Now that we have a moment instance we can easily adjust the time to our liking using
moments built in manipulation methods such as add and subtract.
Now if we want to actually save that new value to the change record we need to format
the value so that ServiceNow can recognize it. ServiceNow stores raw date values as UTC
in the following format "YYYY-MM-DD HH:mm:ss"
We can use moments format() method with this format to ensure we are passing the
correctly formatted value. Updating the change record would look like:
If we run this script in the background we should see the UI show that Planned start has
had 1 day added and 2 hours subtracted.
The entire script should look something like
gs.include("Moment");
Helpful Links
Moment.js docs
UI Scripts
Script Includes
Libraries