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

If youre talking about using moment.js in a service portal widget - then there is no need to include it as a dependency since its already included in the base. If you do "console.log(moment())" in the client controller then you will see it works. If you are trying on the server script however, then yes you need to do the above

nathanclw
Giga Contributor

I get an error when I create the script include -- won't save.

Here are my steps:

1) create a new script include

2) delete the default text so it's empty

3) paste in script from https://momentjs.com/downloads/moment.js

When I try to save, I get the following error:

JavaScript parse error at line (4911) column (52) problem = missing name after . operator (<refname>; line 4911)

This is the code at that line:

    if (typeof Symbol !== 'undefined' && Symbol.for != null) {
        proto[Symbol.for('nodejs.util.inspect.custom')] = function () {
            return 'Moment<' + this.format() + '>';
        };
    }

 

We're running the New York release.

 

Thanks.

priyanga1
Kilo Contributor

did you fix this error?

im getting same error.

find_real_file.png

ryannnnnnnnnnnn
Tera Contributor

Hi @priyanga ,

 

This is how Nathan and I fixed it:  https://stackoverflow.com/questions/55144980/how-to-use-moment-js-in-servicenow/65244342#65244342

 

Read the upvoted answer from "Ryan" 🙂

 

Thanks,
Ryan

priyanga1
Kilo Contributor

Thanks Ryan. Issue is fixed by pasting the content mentioned in MomentJS 2.22.1.

why am I unable to use the new version mentioned in moment.js?