The Wayback Machine - https://web.archive.org/web/20240822105531/https://www.geeksforgeeks.org/moment-js/
Open In App

Moment.js

Last Updated : 16 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Moment.js is a JavaScript package that makes it simple to parse, validate, manipulate, and display date/time in JavaScript. It allows you to display dates in a human-readable way based on your location. It can be used in a browser using the script approach. Moment.js is also compatible with Node.js and can be installed via npm.

Moment.js Tutorial

Why Moment.js?

You may find numerous simple ways to add, subtract, validate dates, retrieve the maximum and minimum dates, and so on in Moment.js. It’s an open-source project, so you can easily contribute to it, and the add functionality in the form of plugins. You can parse the date into the desired format using parsing. We can also perform date validation.

Steps to Install Moment.js

We can install it in the following two ways.

Method 1: Go to the official documentation of https://momentjs.com/ and download the latest moment.js file available and then add the link inside the script tag. Without downloading the file, you can also use the CDN link to run the code.

<script type=”text/JavaScript” src=”https://MomentJS.com/downloads/moment.js”></script>

Method 2: We can install it using npm. Make sure that you have Node.js and npm installed.

npm install moment

Example: In this example, we will get the current time in hours.

index.js
// Importing moment module
const moment = require('moment');

var a = moment().hours();

console.log("Hours is: ",a);

Run index.js file using below command

node index.js

Output:

Hours is:  22


Similar Reads

Moment.js moment().max(Moment|String|Number|Date|Array) Method
The moment().max() method limits the moment to a maximum of another moment value. We can also say that m1.max(m2) is the same as m1 = moment.min(m1, m2). This method is the counterpart of moment.min() method. Syntax: moment().max(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js moment().min(Moment|String|Number|Date|Array) Method
The moment().min() method limits the moment to a minimum of another moment value. We can also say that m1.min(m2) is the same as m1 = moment.max(m1, m2). This method is the counterpart of moment.max() method. Syntax: moment().min(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js moment().week() Method
The moment().week() method is used to get or set the week of the Moment object. It is a locale aware method and hence the week of number will differ based on the locale of the Moment. This varies as the first day of the week can be either a Sunday or Monday according to the Locale. Note: The day of the week would stay the same when this method is u
3 min read
Moment.js moment().toString() Method
The moment().toString() Method is used to return a human-readable string of the Moment object in English. It is similar to the native Date object's toString() method. The date return contains the Weekday, Month, Day, Year, Time and Timezone Offset. Syntax: moment().toString(); Parameters: This method does not accept any parameters: Return Value: Th
2 min read
Moment.js moment().weekYear() Method
The moment().weekYear() Method is used to get or set the current week-year of the Moment. This method is Locale-aware meaning that the first day of the week can be a Sunday or Monday depending on the Locale of the Moment. This may cause the week-year to differ as the first day of the first week may not fall on the first day of the year depending on
2 min read
Moment.js moment().local() Method
The moment().local() method is used to specify that the given Moment object's timezone would be displayed in the local timezone of the user. An optional parameter can be passed that preserves the current time value and only changes the timezone to the local one. Syntax: moment().local( Boolean ); Parameters: This method accepts a single parameter a
2 min read
Moment.js moment().from() Function
The moment().from() function is used to calculate the time from any other time in Node.js. It takes a date as a parameter and calculates the time from that date. Syntax: moment().from(Moment|String|Number|Date|Array, Boolean); Parameters: This function accepts two parameters, first is the date parameter which can be of Moment|String|Number|Date|Arr
2 min read
Moment.js moment().format() Function
The moment().format() function is used to format the date according to the user's need. The format can be provided in string form which is passed as a parameter to this function. Syntax: moment().format(String); Parameters: This function accepts a single parameter of string type, which defines the format. Return Value: This function returns the dat
2 min read
Moment.js moment().daysInMonth() Function
The moment().daysInMonth() function is used to get the number of days in the month of a particular month in Node.js. It returns an integer denoting the number of days. Syntax: moment().daysInMonth(); Parameters: This function has no parameters. Return Value: This function returns an integer. Installation of moment module: You can visit the link to
2 min read
Moment.js moment().valueOf() Function
The moment().valueOf() function is used to get the number of milliseconds since the Unix Epoch. Basically, Unix time is a system for describing a point in time. Syntax: moment().valueOf(); Parameters: This function has no parameters. Return Value: This function returns the Unix Timestamp in milliseconds. Installation of moment module: You can visit
2 min read
Moment.js moment().calendar() Function
The moment().calendar() function is used to displays calendar time which is relative to a given referenceDay. By default, it is set to the start of the current day i.e. today. Syntax: moment().calendar(referenceDay, formats); Parameters: This function has two parameters, the first one is the referenceDay and another one is the format.Return Value:
2 min read
Moment.js moment().to() Function
The moment().to() function is used when users want to display a moment in relation to a time other than now. This function calculates the time to a particular date in Node.js. Syntax: moment().to(Moment|String|Number|Date|Array, Boolean); Parameters: This function has two parameters, first one is of type Moment|String|Number|Date|Array which repres
2 min read
Moment.js moment().toNow() Function
The moment().toNow(Boolean) function is used to calculate the time to now in Node.js. This function is sometimes called time ago or relative time. Syntax: moment().toNow(Boolean); Parameters: This function has one optional boolean type parameter to return the value without a prefix. Return Value: This function returns the date. Installation of mome
2 min read
Moment.js moment().fromNow() Function
The moment().fromNow() function is used to calculate the time from now in Node.js. This function returns the time, spend from the date which is passed in the parameter. Syntax: moment().fromNow(Boolean); Parameters: This function has one optional boolean parameter which is passed to get the value without the suffix. Return Value: This function retu
2 min read
Moment.js moment().millisecond() Method
The moment().millisecond()Method is used to get the milliseconds from the time or to set the milliseconds. Syntax: moment().millisecond(); // or moment().milliseconds(); Parameters: This method accepts single parameter number that is used to set milliseconds then you will require an additional parameter: Number: It ranges from 0 to 1000 and used to
1 min read
Moment.js moment.second() Method
The moment().second() Method is used to get the seconds from the current time or to set the seconds. Syntax: moment().second(); // or moment().seconds(); Parameters: This method accepts a single parameter number that you want to set seconds then you will require an additional parameter: Number: It ranges from 0 to 59 and is used to set seconds in a
1 min read
Moment.js moment.duration().subtract() Method
The moment().duration().subtract() method is used to subtract the given time from a duration. The time to subtract can be another Duration object, value in milliseconds, or a representation of the time using a string and numerical value. All the shorthands and keys used for creating durations can be used here for the time. Syntax: moment().duration
4 min read
Moment.js moment.minute() Method
The moment().minute() Method is used to get the minutes from the current time or to set the minutes. Syntax: moment().minute(); or moment().minutes(); Parameters: This method accepts a single parameter number that you want to set minutes then you will require an additional parameter: Number: It ranges from 0 to 59 and used to set minutes in a varia
1 min read
Moment.js moment.hour() Method
The moment().hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or moment().hours(); Parameters: This method accepts a single parameter number that you want to set hours then you will require an additional parameter: Number::It ranges from 0 to 23 and used to set hours in a variable. Return Va
1 min read
Moment.js moment.duration(x.diff(y)) Method
The moment().duration(x.diff(y)) method is used to create a duration using the difference between two Moment objects by using the diff() method. Syntax: moment().duration( x.diff(y) ); Parameters: This method accepts a single parameter that uses the diff() method to find the difference between two Moment objects. Return Value: This method returns a
1 min read
Moment.js moment().normalizeUnits() Method
The moment().normalizeUnits() method is used to normalize aliases of the units of time supported by Moment. Most of the inbuilt methods of Moment accept these aliases, therefore, this method can be used for extending the library for other methods to maintain consistency with the Moment library. Syntax: moment().normalizeUnits( String ); Parameters:
1 min read
Moment.js moment().inspect() Method
The moment().inspect() method is used for debugging of Moment objects by producing a machine-readable string. This string can be used to generate the same Moment object as the current one. It can be also used in Node interactive shells for displaying the Moment object. Note: In some cases may not work correctly as this method is mostly used for deb
2 min read
Moment.js moment.duration().toISOString() Method
The moment().duration().toISOString() Method is used to get an ISO8601 standard formatted string of the given duration. The UTC mode is used for the timestamp regardless of the locale of the moment, so that consistency can be maintained with the native Date API of JavaScript. This behaviour can be disabled by passing true to the keepOffset paramete
2 min read
Moment.js moment.duration().years() Method
The moment().duration().years() method is used to get the years of the duration. This method returns the number of complete years in the duration, thereby returning a whole number as the value. This method is different from the asYears() method which returns the length of the given duration in years as it can have decimals to denote an incomplete y
2 min read
Moment.js moment.duration().months() Method
The moment().duration().months() method is used to get the month of the duration. This method returns a value between 0 and 11. This method is different from the asMonths() method which returns the length of the given duration in months. Syntax: moment().duration().months(); Parameters: This method does not accept any parameters. Return Value: This
2 min read
Moment.js moment.duration().weeks() Method
The moment().duration().weeks() method is used to get the weeks of the duration. This number of weeks is calculated as a subset of the days, therefore having a value between 0 and 4. The length of days for calculating each week is 7 days. This method is different from the asWeeks() method which returns the length of the given duration in weeks. Syn
2 min read
Moment.js moment.duration().humanize() Method
The moment().duration().humanize() Method is used to return the length of the duration in a human-readable format. The text returned is suffix-less by default, however, suffixes can also be added by specifying the withSuffix parameter. This can also be used for denoting past time by using negative durations. Syntax: moment().duration().humanize(wit
2 min read
Moment.js moment.duration(Number) Method
The moment.duration() function can be used to create durations. Duration objects are defined as intervals of time. Syntax: moment.duration(Number) Parameters: It takes a number as a parameter which is used to signify the length of time in milliseconds. Returns: A Duration object Example 1: C/C++ Code import moment from 'moment'; let minute = 60 * 1
1 min read
Moment.js moment.duration().add() Method
The moment().duration().add() method is used to add a given time to the duration. The time to be added can be another Duration object, value in milliseconds, or a representation of the time using a string and numerical value. All the shorthands and keys used for creating durations can be used here for the time. Syntax: moment().duration().add(Numbe
4 min read
Moment.js moment.duration().locale() Method
The moment().duration().locale() Method is used to return or set the locale of the duration. The locale of a duration will also affect methods like humanize() method that return the time in text, where different locales will affect the language of the text. Syntax: moment().duration().locale(String) Parameters: This method accepts a single paramete
2 min read
three90RightbarBannerImg