0

I am having difficulties including moment.js in my browser code. This is my first time using it and teaching myself to use a library, so any help is appreciated. I have a CDN for moment in my html page:

      <!-- jQuery cdn -->
    <script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

      <!-- moment.js cdn -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/af.js"></script>

      <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <script src="script.js"></script>
    <script type="text/javascript">
      var momentTest = moment(); 
      console.log(momentTest);  // this is not logging
    </script>

Also in my script.js file, I have tried calling it another way with format() like I saw in the docs:

 var now = moment().format();
 console.log(now); //this is not logging either

But I keep getting a console error - Uncaught ReferenceError: moment is not defined. Can anyone help with this? I saw this article already but it didn't help me (total beginner with moment): How to use Moment.js?

Community
  • 1
  • 1
lnamba
  • 1,681
  • 3
  • 18
  • 26
  • It looks like you might be using a bad address for the moment cdn. I tried using `` and it worked fine. Can you try that? – Alex Patch Apr 04 '17 at 16:18

2 Answers2

4

This line does not load moment.js, but loads the af (Afrikaans) locale for moment.js.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/af.js"></script>

To load moment.js, you'd want to load the moment.js file instead:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Frxstrem
  • 38,761
  • 9
  • 79
  • 119
  • Cool thank you so much! I didn't even notice which was the correct one-just picked the top one. Thanks again! – lnamba Apr 04 '17 at 16:44
0

Try this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="script.js"></script>

    <script type="text/javascript">
      var momentTest = moment(); 
      console.log(momentTest);  // this is not logging
    </script>
A. El-zahaby
  • 1,130
  • 11
  • 32
  • Don't use the download link directly as a script. You should instead link to a CDN with moment.js such as [CloudFlare](https://cdnjs.com/libraries/moment.js/), as these services are dedicated to serve scripts that way. – Frxstrem Apr 04 '17 at 16:19