Moment.js moment().min() 方法

Moment.js moment().min() 方法

moment().min() 方法 用于返回给定 Moment 对象数组中的最小值(或最远的过去)。如果没有传入参数,它将返回当前时间的 Moment 实例。

语法:

moment().min( Moment[] );

参数:

该方法接收一个参数,如上所述,并在下面进行描述:

  • Moment[]: 这是一个Moment对象的数组,其中必须返回最小的一个。这是可选参数。

返回值:该方法返回给定Moment对象数组的最小Moment。

注意:

在普通的Node.js程序中,这个方法不起作用,因为需要全局或项目目录中安装外部的moment.js库。

可以使用以下命令安装Moment.js:

安装moment模块:

npm install moment

下面的示例将演示 Moment.js moment().min() 方法。

示例1:

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() 
)

输出:

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

示例2:

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() 
)

输出:

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

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程