我在节点中使用'rrule.js‘库来解析我的RRule。我想知道今天和规则日是不是一样。它适用于大多数情况,但不是所有情况。我还使用moment.js进行比较。问题出在"rule.after()“中。它应该包括当天,但它没有。
function checkIfToday(rruleStr){
var RRule = require('rrule').RRule;
var moment = require('moment');
var rule = RRule.fromString(rruleStr);
// Convert all dates into UTC before comparison
var todayutc = moment().utc(); // today in UTC
var nextOccurrence = rule.after(todayutc,inc=true); // next rule date including today
var nextOccurutc = moment(nextOccurrence).utc(); // convert today into utc
var match = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same
return match;
}你知道最好的方法是什么吗?
谢谢。
发布于 2016-05-25 01:31:36
这对我很有效。尝试使用moment的startOf方法将todayutc的时间设置回一天的开始:
function checkIfToday(rruleStr){
var RRule = require('rrule').RRule;
var moment = require('moment');
var rule = RRule.fromString(rruleStr);
// Convert all dates into UTC before comparison
var todayutc = moment().utc().startOf('day'); // today in UTC
var nextOccurrence = rule.after(todayutc, true); // next rule date including today
var nextOccurutc = moment(nextOccurrence).utc(); // convert today into utc
var match = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same
return match;
}https://stackoverflow.com/questions/29953152
复制相似问题