Open In App

Moment.js moment().max() Method

The moment().max() method is used to return the maximum (or the most distant future) Moment of the given array of Moment objects. It will return the Moment instance of the current time if no argument is passed.

Syntax:



moment().max( Moment[] );

Parameters: This method accepts a single parameter as mentioned above and described below:

Return Value: This method returns the maximum Moment of the given array of Moment objects.



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.

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:




const moment = require('moment');
  
let momentOne = moment();
let momentTwo = moment().add(15, 'days');
let momentThree = moment().add(15, 'months');
  
console.log(
`The three Moments are:
    ${momentOne}
    ${momentTwo}
    ${momentThree}
`);
  
let maximumMoment = 
    moment.max([momentOne, momentTwo, momentThree]);
console.log(
    "The maximum Moment of the above is:", maximumMoment
)

Output:

The three Moments are:
    Mon Jul 11 2022 01:15:34 GMT+0530
    Tue Jul 26 2022 01:15:34 GMT+0530
    Wed Oct 11 2023 01:15:34 GMT+0530

The maximum Moment of the above is: 
    Moment<2023-10-11T01:15:34+05:30>

Example 2:




const moment = require('moment');
  
let momentA = moment();
let momentB = moment().add(15, 'hours');
let momentC = moment().add(1000, 'seconds');
  
console.log(
`The three Moments are:
    ${momentA}
    ${momentB}
    ${momentC}
`);
  
let maximumMoment2 = 
    moment.max([momentA, momentB, momentC]);
console.log(
    "The maximum Moment of the above is:", maximumMoment2
)

Output:

The three Moments are:
    Mon Jul 11 2022 01:15:34 GMT+0530
    Mon Jul 11 2022 16:15:34 GMT+0530
    Mon Jul 11 2022 01:32:14 GMT+0530

The maximum Moment of the above is: 
    Moment<2022-07-11T16:15:34+05:30>

Reference: https://momentjs.com/docs/#/get-set/max/


Article Tags :