Open In App

Moment.js moment().min() Method

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().min() method is used to return the minimum (or the most distant past) 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().min( Moment[] );

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

  • Moment[]: This is an array of Moment objects from which the minimum one has to be returned. it is an optional parameter.

Return Value: This method returns the minimum 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().min() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = moment();
let momentTwo = moment().subtract(15, 'days');
let momentThree = moment().subtract(25, 'days');
  
console.log(
`The three Moments are:
    ${momentOne}
    ${momentTwo}
    ${momentThree}
`);
  
let minimumMoment =
    moment.min([momentOne, momentTwo, momentThree]);
console.log(
    "The minimum of the Moments from the above is:",
    minimumMoment.toString()
)


Output:

The three Moments are:
    Mon Jul 11 2022 01:12:38 GMT+0530
    Sun Jun 26 2022 01:12:38 GMT+0530
    Thu Jun 16 2022 01:12:38 GMT+0530

The minimum of the Moments from the above is:
     Thu Jun 16 2022 01:12:38 GMT+0530

Example 2:

Javascript




let momentA = moment();
let momentB = moment().subtract(20, 'seconds');
let momentC = moment().subtract(120, 'milliseconds');
  
console.log(
`The three Moments are:
    ${momentA}
    ${momentB}
    ${momentC}
`);
  
let minimumMoment2 = 
    moment.min([momentA, momentB, momentC]);
console.log(
    "The minimum of the Moments from the above is:",
    minimumMoment2.toString()
)


Output:

The three Moments are:
    Mon Jul 11 2022 01:12:38 GMT+0530
    Mon Jul 11 2022 01:12:18 GMT+0530
    Mon Jul 11 2022 01:12:38 GMT+0530

The minimum of the Moments from the above is:
     Mon Jul 11 2022 01:12:18 GMT+0530

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads