如何使用忽略年份值的 momentJS 比较两个日期?
How to compare two dates with momentJS ignoring the year value?
假设一个学期从 2015 年 11 月 1 日开始到 2016 年 1 月 3 日。要比较的样本日期如下 ('YYYY-MM-DD'):
2015-10-12 = false
2015-11-01 = true (inclusive)
2015-12-20 = true
2015-01-03 = true (inclusive)
2016-01-30 = false
2017-11-21 = true (year is ignored)
2010-12-20 = true (year is ignored)
有什么方法可以用 MomentJS 实现这个结果吗?
可以使用 isBetween
,但有点乱。
function isWithinTerm(dateString) {
var dateFormat = '____-MM-DD', // Ignore year, defaults to current year
begin = '2015-10-31', // Subtract one day from start of term
end = '2016-01-04', // Add one day to finish of term
mom = moment(dateString, dateFormat); // Store to avoid re-compute below
return mom.isBetween(begin, end) || mom.add(1, 'y').isBetween(begin, end);
}
我添加一年作为可选检查的原因仅适用于 1 月的情况,因为 2015 年 1 月显然不在 2015 年 11 月和 2016 年 1 月之间。我知道这有点老套,但我想不通任何更简单的方法。
它会像这样工作:https://jsfiddle.net/3xxe3Lg0/
var moments = [
'2015-10-12',
'2015-11-01',
'2015-12-20',
'2015-01-03',
'2016-01-30',
'2017-11-21',
'2010-12-20'];
var boundaries = [moment('2015-11-01').subtract(1, 'days'),moment('2016-01-03').add(1, 'days')];
for (var i in moments){
res = moments[i] + ': ';
if (
moment(moments[i]).year(boundaries[0].year()).isBetween(boundaries[0], boundaries[1]) ||
moment(moments[i]).year(boundaries[1].year()).isBetween(boundaries[0], boundaries[1])
){
res += 'true';
}
else{
res += 'false';
}
$('<div/>').text(res).appendTo($('body'));
}
编辑:即使上限不是一年而是比下限提前两年(或更长时间),它甚至可以工作。
for (var i in moments){
res = moments[i] + ': ';
if (
moment(moments[i]).year(boundaries[0].year()).isBetween(boundaries[0], boundaries[1]) ||
moment(moments[i]).year(boundaries[0].year()+1).isBetween(boundaries[0], boundaries[1])
){
res += 'true';
}
else{
res += 'false';
}
$('<div/>').text(res).appendTo($('body'));
}
假设一个学期从 2015 年 11 月 1 日开始到 2016 年 1 月 3 日。要比较的样本日期如下 ('YYYY-MM-DD'):
2015-10-12 = false
2015-11-01 = true (inclusive)
2015-12-20 = true
2015-01-03 = true (inclusive)
2016-01-30 = false
2017-11-21 = true (year is ignored)
2010-12-20 = true (year is ignored)
有什么方法可以用 MomentJS 实现这个结果吗?
可以使用 isBetween
,但有点乱。
function isWithinTerm(dateString) {
var dateFormat = '____-MM-DD', // Ignore year, defaults to current year
begin = '2015-10-31', // Subtract one day from start of term
end = '2016-01-04', // Add one day to finish of term
mom = moment(dateString, dateFormat); // Store to avoid re-compute below
return mom.isBetween(begin, end) || mom.add(1, 'y').isBetween(begin, end);
}
我添加一年作为可选检查的原因仅适用于 1 月的情况,因为 2015 年 1 月显然不在 2015 年 11 月和 2016 年 1 月之间。我知道这有点老套,但我想不通任何更简单的方法。
它会像这样工作:https://jsfiddle.net/3xxe3Lg0/
var moments = [
'2015-10-12',
'2015-11-01',
'2015-12-20',
'2015-01-03',
'2016-01-30',
'2017-11-21',
'2010-12-20'];
var boundaries = [moment('2015-11-01').subtract(1, 'days'),moment('2016-01-03').add(1, 'days')];
for (var i in moments){
res = moments[i] + ': ';
if (
moment(moments[i]).year(boundaries[0].year()).isBetween(boundaries[0], boundaries[1]) ||
moment(moments[i]).year(boundaries[1].year()).isBetween(boundaries[0], boundaries[1])
){
res += 'true';
}
else{
res += 'false';
}
$('<div/>').text(res).appendTo($('body'));
}
编辑:即使上限不是一年而是比下限提前两年(或更长时间),它甚至可以工作。
for (var i in moments){
res = moments[i] + ': ';
if (
moment(moments[i]).year(boundaries[0].year()).isBetween(boundaries[0], boundaries[1]) ||
moment(moments[i]).year(boundaries[0].year()+1).isBetween(boundaries[0], boundaries[1])
){
res += 'true';
}
else{
res += 'false';
}
$('<div/>').text(res).appendTo($('body'));
}