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 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.
Javascript
const moment = require( 'moment' );
let momentOne = moment();
let momentTwo = moment().subtract(15, 'days' );
console.log(
`The two Moments are:
${momentOne}
${momentTwo}
`);
let finalMoment =
momentOne.max(momentTwo);
console.log(
"The calculated Moments from the above is:" ,
finalMoment.toString()
)
|
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.
Javascript
const moment = require( 'moment' );
let momentOne = moment( new Date( "01-01-2001" ));
let momentTwo = moment( new Date( "02-02-2002" ));
console.log(
`The two Moments are:
${momentOne}
${momentTwo}
`);
let finalMoment =
momentOne.max(momentTwo);
console.log(
"The calculated Moments from the above is:" ,
finalMoment.toString()
)
|
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.
Javascript
const moment = require( 'moment' );
let momentOne = moment();
let momentTwo = moment().subtract(15, 'days' );
let momentThree = moment().add(15, 'days' );
console.log(
`The three Moments are:
${momentOne}
${momentTwo}
${momentThree}
`);
let finalMoment =
momentOne.max(momentTwo , momentThree);
console.log(
"The calculated Moment from the above is:" ,
finalMoment.toString()
)
|
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/
Last Updated :
02 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...