Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi,

I also copied the code from moment.js.

Used even the same example in the background script :

 

var newYork    = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london     = newYork.clone().tz("Europe/London");

newYork.format();    // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format();     // 2014-06-01T17:00:00+01:00

How to get the output for the same? Kindly assist.


Regards,
Sundar