![]() Instead, we would like to recommend alternatives that are excellent choices for use in modern applications today. We recognize that many existing projects may continue to use Moment, but we would like to discourage Moment from being used in new projects going forward. The Moment team has discussed these issues at length. 4 alternatives to moment.js for internationalizing dates.You Probably Don't Need Moment.js Anymore. ![]() Recently, Chrome Dev Tools started showing recommendations for replacing Moment for the size alone. Libraries like Luxon (and others) take advantage of this, reducing or removing the need to ship your own data files. Modern web browsers (and Node.js) expose internationalization and time zone support via the Intl object, codified as ECMA-402. If one needs internationalization or time zone support, Moment can get quite large. Moment doesn't work well with modern "tree shaking" algorithms, so it tends to increase the size of web application bundles. Since this has already been accomplished in other libraries, we feel that it is more important to retain the mutable API.Īnother common argument against using Moment in modern applications is its size. We address it in our usage guidance but it still comes as a surprise to most new users.Ĭhanging Moment to be immutable would be a breaking change for every one of the projects that use it.Ĭreating a "Moment v3" that was immutable would be a tremendous undertaking and would make Moment a different library entirely. This is a common source of complaints about Moment. ![]() Given how many projects depend on it, we choose to prioritize stability over new features.Īs an example, consider that Moment objects are mutable. Moment has evolved somewhat over the years, but it has essentially the same design as it did when it was created in 2011. The modern web looks much different these days. toString() on a Date object (but the resulting string is not in the same ISO 8601 format).Moment.js has been successfully used in millions of projects, and we are happy to have contributed to making date and time better on the web.Īs of September 2020, Moment gets over 12 million downloads per week! However, Moment was built for the previous era of the JavaScript ecosystem. format() on a Moment object in local mode. If you want the time zone reflected in the string output, you must use a function that produces a string with respect to local time. The Date object is inherently UTC-based, your output is also UTC-based, and UTC is the same over the whole planet (by design). No amount of changing your time zone settings will change this result. The point being, you can't rely on the behavior of console.log(Date) to be consistent. It's entirely possible some future implementation could show the result in some graphical or interactive output. toString() on the Date object and log that, the result being in local time. toISOString() displays the result in UTC, the result of logging a Date object is also shown in UTC. Some implementations, like in your example, will call. Implementations of ECMAScript can do whatever they like in this regard. Here's the interesting part: There is no spec for this behavior. You then pass the string to console.log - except one can't just log an object, so it first has to convert it to something so you can see it. Since you derived the Moment object from the current time, the result is the same as if you just called new Date() to begin with. Thus, the timestamp within the Moment object becomes the timestamp for the resulting Date object. The "mode" of the moment object is no longer relevant because Date objects don't track anything other than a timestamp. console.log(now.toDate()) // T18:23:50.916Zīy calling toDate, you ask the Moment object to create a Date object. You then pass that string to console.log, which emits it to the console. In this case, the local time is two hours ahead of UTC. ![]() Since it's in local mode, the offset that applies to that moment in time for the local time zone is emitted in the result, and the wall time shown in the result is adjusted for that offset. console.log(now.format()) // T20:23:50+02:00īy calling format, you ask the Moment object to produce a String. You don't pass any parameters, so it is initialized using the current timestamp (as if you called Date.now()) and set to "local mode". Let's walk through your code example: let now = moment()
0 Comments
Leave a Reply. |