Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"moment is not defined" using moment.js

Tags:

I am working with moment.js, but as I am testing the moment.js library I continue to receive an error.

var back30Days=moment().subtract(30, 'days').format("dddd, MMMM Do YYYY, h:mm:ss p"); 

it returns "moment not defined." referring to .format("dddd, MMMM Do YYYY, h:mm:ss p");

I've read the documentation, and everything looks fine, but when I reload my page, the js I'm working with won't load.

any tips would be greatly appreciated!

like image 692
alej_dra Avatar asked Jun 17 '17 20:06

alej_dra


People also ask

How do you define a moment in JavaScript?

Creating a moment object with a specific date and time is possible by calling the moment() function with arguments. Like the JavaScript Date, a moment object can be created from the number of milliseconds since 1970/1/1. Another possibility is using an array [year, month, day, hour, minute, second, milliseconds] .

Why you shouldnt use moment JS?

However, Moment. js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn't support tree shaking. Even the Moment team discourages to use their library in new projects.

Is moment JS still used?

The people behind Moment. js are even advising against using it. We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.

Is moment JS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.


1 Answers

With latest momentjs:

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

var back30Days=moment().subtract(30, 'days').format("dddd, MMMM Do YYYY, h:mm:ss p");    console.log('back30Days --> ' + back30Days);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
like image 192
Dalin Huang Avatar answered Sep 19 '22 19:09

Dalin Huang