How to import and use moment.js?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2017 03:52 AM
I am not an expert on Javascript, so could a more experienced JS developer help me out? I want to output date in a given format, so I was looking at the moment.js library. I copy pasted the code from 'moment.js' in a script include and create the following code, but it's not working and I don't know why...
gs.include('moment.js');
for (var x in moment) gs.print(x)
gs.print(moment().format());
Output is:
*** Script: deprecationHandler
*** Script: min
*** Script: relativeTimeThreshold
*** Script: defaultFormatUtc
*** Script: monthsShort
*** Script: version
*** Script: calendarFormat
*** Script: defaultFormat
*** Script: now
*** Script: createFromInputFallback
*** Script: isDuration
*** Script: isMoment
*** Script: unix
*** Script: invalid
*** Script: weekdaysShort
*** Script: defineLocale
*** Script: locales
*** Script: localeData
*** Script: updateOffset
*** Script: relativeTimeRounding
*** Script: utc
*** Script: fn
*** Script: weekdays
*** Script: normalizeUnits
*** Script: duration
*** Script: isDate
*** Script: locale
*** Script: months
*** Script: max
*** Script: parseZone
*** Script: weekdaysMin
*** Script: RFC_2822
*** Script: updateLocale
*** Script: ISO_8601
*** Script: momentProperties
*** Script: suppressDeprecationWarnings
*** Script: lang
*** Script: langData
*** Script: parseTwoDigitYear
Javascript compiler exception: The undefined value has no properties. (sys_script_include.1ed8b187373f7a00a0858d2754990e3f; line 26) in:
gs.include('moment.js');
for (var x in moment) gs.print(x)
gs.print(moment().format());
So, the code is loaded correctly. It knows the moment variable, but I cannot create a new object from it... What am I doing wrong...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2017 05:46 AM
I copied and pasted the moment.js code from this CDN: https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js, then ran your code and it worked fine. Maybe it has to do with the version of moment you used.
Here was my output:
*** 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: 2017-07-11T05:44:32-07:00
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2017 05:55 AM
That's really weird... Might it be since we are still in Geneva and using an older Ecmascript?
I traced the problem to 'X instanceof Y'. If X is undefined it crashes. If I run 'undefined instanceof Array' in a background script for example I get the same error.
In the end I got the code to work by giving the constructor a date (JS date). It just crashes for empty values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2019 01:56 PM
Hi, i'm actually having issues simply loading the moment.js library into ServiceNow, any chance you can provide some instructions on how you did this? thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2019 09:55 AM
Depending on how you plan on using it, you can load it as a UI Script or as a Widget Dependency for Service Portal.