The CreatorCon Call for Content is officially open! Get started here.

How to use Moment.js in ServiceNow

yundlu316
Kilo Guru

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:

  1. 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?
  2. 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?
  3. 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!

12 REPLIES 12

Jace Benson
Mega Sage

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...

Jamison Cote2
Mega Expert

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

 

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

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.