📜  Moment.js isSameOrBefore()函数

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

Moment.js isSameOrBefore()函数

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

句法:

moment().isSameOrBefore(Moment|String|Number|Date|Array);
moment().isSameOrBefore(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')
    .isSameOrBefore('2010-10-21');  // true
console.log(bool1);
  
var bool2 = moment('2010-10-20')
    .isSameOrBefore('2010-10-20');  // true
console.log(bool2);
  
var bool3 = moment('2010-10-20')
    .isSameOrBefore('2010-10-19');  // false
console.log(bool3);

运行程序的步骤:

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

    输出:

    true
    true
    false
    

示例 2:文件名:index.js

// Requiring module
const moment = require('moment');
  
function checkIsSameOrBefore(date1, date2) {
   return moment(date1).isSameOrBefore(date2);  
}
  
var bool = checkIsSameOrBefore
    ('2009-10-20', '2010-10-20');  // true
console.log(bool);

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

node index.js

输出:

true

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