How to import and use moment.js?

peterraeves
Mega Guru

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

5 REPLIES 5

Justin Abbott
Giga Guru

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



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.


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!

Depending on how you plan on using it, you can load it as a UI Script or as a Widget Dependency for Service Portal.