VOOZH about

URL: https://www.geeksforgeeks.org/node-js/moment-js-moment-maxmomentstringnumberdatearray-method/

⇱ Moment.js moment().max(Moment|String|Number|Date|Array) Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Moment.js moment().max(Moment|String|Number|Date|Array) Method

Last Updated : 2 Sep, 2022

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 described below:

  • Moment: This is a Moment object by which the moment one has to be limited.
  • String: This is a String value that is used for comparison.
  • Number: We can also pass a numeric value for comparison.
  • Date: This is a Date object by which the moment one has to be limited.
  • Array: This is the Array in which the moment objects are compared.

Return Value: This method returns the Moment object calculated after a comparison of the given parameters.

Note:

  • This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.
  • This method is deprecated and it is recommended to use the current moment.min() method instead.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment().max() Method.

Example 1: In this example, we are comparing current moment with the moment 15 days before the current date.

Output:

The two Moments are:
 Mon Aug 29 2022 13:27:39 GMT+0530
 Sun Aug 14 2022 13:27:39 GMT+0530
The calculated Moments from the above is: 
 Sun Aug 14 2022 13:27:39 GMT+0530

Example 2: In this example, we are comparing dates, this method will return the minimum of both the dates.

Output:

The two Moments are:
 Mon Jan 01 2001 00:00:00 GMT+0530
 Sat Feb 02 2002 00:00:00 GMT+0530
The calculated Moments from the above is: 
 Mon Jan 01 2001 00:00:00 GMT+0530

Example 3: In this example, we are comparing the array of moments.

Output:

The three Moments are:
 Mon Aug 29 2022 13:30:50 GMT+0530
 Sun Aug 14 2022 13:30:50 GMT+0530
 Tue Sep 13 2022 13:30:50 GMT+0530
The calculated Moment from the above is: 
 Sun Aug 14 2022 13:30:50 GMT+0530

Reference: https://momentjs.com/docs/#/manipulating/max/

Comment

Explore