Moment.js moment().min(Moment|String|Number|Date|Array) Method
Last Updated :
02 Sep, 2022
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 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 moment.max() 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().min() Method.
Example 1: In this example, we are comparing the 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.min(momentTwo);
console.log(
"The calculated Moments from the above is:" ,
finalMoment.toString()
)
|
Output:
The two Moments are:
Mon Aug 29 2022 13:05:38 GMT+0530
Sun Aug 14 2022 13:05:38 GMT+0530
The calculated Moments from the above is:
Mon Aug 29 2022 13:05:38 GMT+0530
Example 2: In this example, we are comparing dates, this method will return the maximum 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.min(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:
Sat Feb 02 2002 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.min(momentTwo , momentThree);
console.log(
"The calculated Moment from the above is:" ,
finalMoment.toString()
)
|
Output:
The three Moments are:
Mon Aug 29 2022 13:15:34 GMT+0530
Sun Aug 14 2022 13:15:34 GMT+0530
Tue Sep 13 2022 13:15:34 GMT+0530
The calculated Moment from the above is:
Mon Aug 29 2022 13:15:34 GMT+0530
Reference: https://momentjs.com/docs/#/manipulating/min/
Share your thoughts in the comments
Please Login to comment...