How to use Moment.js in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2019 06:33 AM
Our team is trying to use Moment.js in our instance, but can't seem to get it to work. Here are a couple questions we have about it:
- We noticed that there is a dependency out of the box called moment-timezone-with-data-2010-2020-v0.5, is this the same as moment.js? If so, does this mean we don't need to bring in moment.js as a new dependency?
- We tried using the above ootb dependency AND tried to bring in moment.js to use in a widget, and we keep getting a console error saying that moment is undefined. Can someone provide some instructions on how to correctly get either one of these dependencies to work?
- If we wanted to use moment.js on a platform business rule, what do we have to do to make that happen? Are you able to access a dependency via business rule?
Thanks!
- 7,898 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2019 08:06 PM
They are seperate. On the LCHH videos Josh Nerius and Dave Slusher use it on camera and show how to import and use.
Here's a link to where they are importing the timezone file you mention https://youtu.be/JrVGodtzY3U?t=1215
Here's the post about it. https://developer.servicenow.com/blog.do?p=/post/live-coding-happy-hour-for-2017-02-17-time-math-wit...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2019 10:45 AM
Here is how to use 'moment.js' in ServiceNow. (You can do the same with lodash.js or other JS Libraries)
You first need to locate the minified version of the JS Library and then install that as a Script Include in your platform.
So do this:
1) Create a Script Include called "moment.js" and delete the initial class creation format
DELETE THE BELOW THAT IS AUTO INSERTED UPON CREATION
var moment.js = Class.create();
moment.js.prototype = {
initialize: function() {
},
type: 'moment.js'
};
Then you have a blank script include file. At this point, you can import the moment.js library into ServiceNow:
Go here to download the entire moment.js library file (as of April 20, 2019):
(It is a large JavaScript file that you cut/paste into ServiceNow)
https://momentjs.com/downloads/moment.js
Once you have the Script Include with the moment.js library, you can then use it on the server by referencing the library using ServiceNow's gs.include() method.
To test it out, go into a "Background Script" and run the following:
gs.include('moment.js');
for (var iterate in moment) {
gs.print(iterate);
};
The below should be pushed out in the results:
*** Script: momentProperties
*** Script: suppressDeprecationWarnings
*** Script: deprecationHandler
*** Script: parseTwoDigitYear
*** Script: createFromInputFallback
*** Script: ISO_8601
*** Script: RFC_2822
*** Script: updateOffset
*** Script: defaultFormat
*** Script: defaultFormatUtc
*** Script: lang
*** Script: langData
*** Script: version
*** Script: fn
*** Script: min
*** Script: max
*** Script: now
*** Script: utc
*** Script: unix
*** Script: months
*** Script: isDate
*** Script: locale
*** Script: invalid
*** Script: duration
*** Script: isMoment
*** Script: weekdays
*** Script: parseZone
*** Script: localeData
*** Script: isDuration
*** Script: monthsShort
*** Script: weekdaysMin
*** Script: defineLocale
*** Script: updateLocale
*** Script: locales
*** Script: weekdaysShort
*** Script: normalizeUnits
*** Script: relativeTimeRounding
*** Script: relativeTimeThreshold
*** Script: calendarFormat
*** Script: HTML5_FMT

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2019 12:42 PM
Hello,
Is this something that can be used within a Client Script/HR Scoped Application? And if so, how would it need to be added?
When I test the Background script suggested, I receive:
Evaluator: org.mozilla.javascript.EcmaError: "moment" is not defined.
Caused by error in script at line 3
1: gs.include('moment.js');
2:
==> 3: for (var iterate in moment) {
4: gs.print(iterate);
5: };
Thanks,
-Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2019 05:10 PM
I couldn't get moment.js to work as a widget dependency for my widget. I created the dependency and linked the js include as a URL. So I tried this method of creating the script include and using gs.include in my server script of the widget and it worked just fine. I'm on New York Patch 2 btw. Thanks for the helpful info.