📜  Moment.js isBefore()函数

📅  最后修改于: 2022-05-13 01:56:41.209000             🧑  作者: Mango

Moment.js isBefore()函数

它用于检查日期是否早于 Node.js 中的特定日期,使用isBefore()函数检查一个时刻是否在另一个时刻之前。第一个参数将被解析为片刻,如果还没有的话。

句法:

moment().isBefore(Moment|String|Number|Date|Array);
moment().isBefore(Moment|String|Number|Date|Array, String);

参数:它可以包含 Moment|String|Number|Date|Array。

返回:真或假

力矩模块的安装:

  1. 您可以访问安装时刻模块的链接。您可以使用此命令安装此软件包。
    npm install moment
  2. 安装moment模块后,您可以使用命令在命令提示符下检查您的moment版本。
    npm version moment
  3. 之后,您可以创建一个文件夹并添加一个文件,例如index.js 。要运行此文件,您需要运行以下命令。
    node index.js

示例 1:文件名:index.js

// Requiring module
const moment = require('moment');
  
var bool1 = moment('2010-10-20')
    .isBefore('2010-10-21'); // true
console.log(bool1);
  
var bool2 = moment('2010-10-20')
    .isBefore('2010-12-31', 'year'); // false
console.log(bool2);

运行程序的步骤:

  1. 项目结构将如下所示:
  2. 确保您已使用以下命令安装了moment模块:
    npm install moment
  3. 使用以下命令运行 index.js 文件:
    node index.js

    输出:

    true
    false
    

示例 2:文件名:index.js

// Requiring module
const moment = require('moment');
  
function checkIsBefore(date1, date2) {
    return moment(date1).isBefore(date2); 
}
  
var bool = checkIsBefore('2010-10-20', '2010-10-21');
console.log(bool);

使用以下命令运行 index.js 文件:

node index.js

输出:

true

参考: https://momentjs.com/docs/#/query/is-before/