As an example, consider that Moment objects are mutable. This is a common source of complaints about Moment. We address it in our usage guidance but it still comes as a surprise to most new users. Changing Moment to be immutable would be a breaking change for every one of the projects that use it. Creating a that was immutable would be a tremendous undertaking and would make Moment a different library entirely. Since this has already been accomplished in other libraries, we feel that it is more important to retain the mutable API.
Another common argument against using Moment in modern applications is its size. Moment doesn`t work well with modern algorithms, so it tends to increase the size of web application bundles. If one needs internationalization or time zone support, Moment can get quite large. Modern web browsers (and Node.js) expose internationalization and time zone support via the Intl object, codified as ECMA-402. Libraries like Luxon (and others) take advantage of this, reducing or removing the need to ship your own data files.
When using Date objects, be aware of the following:
The Date object internally represents a Unix timestamp with millisecond precision. It offers functions that will convert to and from the system`s local time zone, but it is always UTC internally. Unlike a Moment object, it can not be set to use another time zone; It has no concept of .
Using Date.parse, or new Date(string) has been problematic and implemented inconsistently in the past. The current specification defines parsing a variation of ISO 8601 strings, where date-only forms (like ) are parsed as UTC, instead of local time as they would be by ISO 8601. Even then, not all modern implementations have implemented this specification correctly (e.g., Safari). Other types of strings may work, but parsing them is implementation specific and can vary significantly - especially with older browsers. Depending on the implementation, and the components provided in the string, you may be surprised with the result. For these reasons, we agree with MDN`s statement that parsing strings with the Date object is strongly discouraged.
When using the Intl object, be aware of the following:
If the Date and Intl objects meet your needs and you fully understand their limitations, then you might consider using them directly.
Temporal will be a new global object that acts as a top-level namespace (like Math). It exposes many separate types of objects including Temporal.Absolute, Temporal.DateTime, Temporal.Date, Temporal.Time, Temporal.TimeZone and several others. The Temporal Cookbook shows many with examples of how these objects can be used in different scenarios.
Note: In 2.4.0, the globally exported moment object was deprecated. It will be removed in next major release.
Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object. To get this wrapper object, simply call moment() with one of the supported input types.
You can use strict mode, which will identify the parsing error and set the Moment object as invalid:
You can create a moment by specifying some of the units in an object.
Note: Despite Unix timestamps being UTC-based, this function creates a moment object in local mode. If you need UTC, then subsequently call .utc(), as in:
This clones the Date object; further changes to the Date won`t affect the Moment, and vice-versa.
Moment`s string parsing functions like moment(string) and moment.utc(string) accept offset information if provided, but convert the resulting Moment object to local or UTC time. In contrast, moment.parseZone() parses the string but keeps the resulting Moment object in a fixed-offset timezone with the provided offset in the string.
You can check whether the Moment considers the date invalid using momentisValid. You can check the metrics used by isValid using momentparsingFlags, which returns an object.
After a moment object is created, all of the inputs can be accessed with creationData() method:
You can create a moment object specifying only some of the units, and the rest will be defaulted to the current day, month or year, or 0 for hours, minutes, seconds and milliseconds.
These map to the corresponding function on the native Date object.
If you want to add multiple different keys at the same time, you can pass them in as an object literal.
Getting the utcOffset of the current object:
Setting the UTC offset by supplying minutes. The offset is set on the moment object that utcOffset() is called on. If you are wanting to set the offset globally, try using moment-timezone. Note that once you set an offset, itt change on its own (i.e there are no DST rules). If you want an actual time zone - time in a particular location, like America/Los_Angeles, consider moment-timezone.
Note: From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.
From version 2.10.3, if the target moment object is invalid the result is the localized Invalid date string.
Note: From version 2.25.0 you can only pass a formats argument, it could be an object of strings and functions:
To get a copy of the native Date object that Moment.js wraps, use momenttoDate.
When serializing an object to JSON, if there is a Moment object, it will be represented as an ISO8601 string, adjusted to UTC.
This returns an object containing year, month, day-of-month, hour, minute, seconds, milliseconds.
Returns a machine readable string, that can be evaluated to produce the same moment. Because of the name its also used in node interactive shell to display objects.
Note: As of version 2.14.0 this function is deprecated. It doesn`t give the right answer after modifying the moment object. For more information refer to moment/3160
Note: before 2.3.0, Moment objects in this condition always returned false for momentisValid; they now return true.
To check if a variable is a moment object, use moment.isMoment().
From version 2.11.0, you can also test for a moment object by instanceof operator:
To check if a variable is a native js Date object, use moment.isDate().
The returned object has the following methods:
From version 2.11.0 months can also be an object, specifying standalone and format forms (nominative and accusative). The regular expression that is run on the format to check whether to use the format form is /D[oD]?(\[[^\[\]]\]|\s+)+MMMM?/. From version 2.14.0 a different one can be specified with the isFormat key.
Note: From version 2.11.0, like Localemonths, LocalemonthsShort can be an object with standalone and format cases.
Note: From version 2.11.0 format/standalone cases can be passed as well. isFormat will be used against the full format string to determine which form to use.
LocalelongDateFormat should be an object containing a key/value pair for each long date format L LL LLL LLLL LT LTS. LT should be the time format, and is also used for momentcalendar.
LocalerelativeTime should be an object of the replacement strings for momentfrom.
The key argument refers to the replacement key in the LocalerelativeTime object. (eg. s m mm h, etc.)
Moment.js also has duration objects. Where a moment is defined as a single point in time, a duration is defined as a length of time.
Much like momentadd, you can pass an object of values if you need multiple different units of measurement.
Create a clone of a duration. Durations are mutable, just like moment objects, so this lets you get a snapshot, at some point in time.
When serializing a duration object to JSON, it will be represented as an ISO8601 string.
To check if a variable is a moment duration object, use moment.isDuration().
You can create your own invalid Moment objects, which is useful in making your own parser.
invalid also accepts an object which specifies which parsing flags to set. This will not set the userInvalidated parsing flag unless it`s one of the properties specified.
At its simplest, just call the fquarter method on any moment object. It returns a formatted string with April being the first quarter.
At Twitter we serve many objects as JSON, including Tweets and Users. These objects all encapsulate core attributes that describe the object. Each Tweet has an author, a message, a unique ID, a timestamp of when it was posted, and sometimes geo metadata shared by the user. Each User has a Twitter name, an ID, a number of followers, and most often an account bio.
With each Tweet we also generate objects, which are arrays of common Tweet contents such as hashtags, mentions, media, and links. If there are links, the JSON payload can also provide metadata such as the fully unwound URL and the webpage`s title and description.
The following JSON illustrates the structure for these objects and some of their attributes:
When ingesting Tweet data the main object is the Tweet Object, which is a parent object to several child objects. For example, all Tweets include a User object that describes who authored the Tweet. If the Tweet is geo-tagged, there will a object included. Every Tweet includes an object that encapsulates arrays of hashtags, user mentions, URLs, cashtags, and native media. If the Tweet has any ‘attached’ or ‘native’ media (photos, video, animated GIF), there will be an object.
We offer objects full
objects full, objects, full,