Moment.js

作者: 洪锦一 | 来源:发表于2023-06-14 10:01 被阅读0次

1. 某个日期,是否在开始日期和结束日期之间

您可以使用Moment.js的isBetween()方法来检查某个日期是否在开始日期和结束日期之间。以下是使用Moment.js的示例代码:

// 设置开始日期和结束日期
const startDate = moment('2023-06-01');
const endDate = moment('2023-06-30');

// 要检查的日期
const checkDate = moment('2023-06-15');

// 检查日期是否在开始日期和结束日期之间
const isBetween = checkDate.isBetween(startDate, endDate);

if (isBetween) {
  console.log('该日期在开始日期和结束日期之间');
} else {
  console.log('该日期不在开始日期和结束日期之间');
}

请注意,Moment.js的isBetween()方法默认包括开始日期和结束日期。如果您想要排除这些日期,请将第三个参数设置为[],如下所示:
[ 表示包含。 ( 表示排除。 如果使用包容性参数,则必须传入两个指示符。

moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '[)'); //true
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '()'); //false
moment('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '(]'); //true
moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]'); //true

相关文章

网友评论

      本文标题:Moment.js

      本文链接:https://www.haomeiwen.com/subject/scwhydtx.html