3

I am using 'rrule.js' library in node to parse my RRule. I would like to know if current day is same as rule day. It works on most cases but not all. I also use moment.js to compare. The issue is in "rule.after()". It should include the current day but it doesn't.

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;
}

Any idea what's the best way to do this.

Thanks.

datau
  • 31
  • 3

1 Answers1

0

This worked for me. Try setting the time of todayutc back to the beginning of the day using moment's startOf method:

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;
}
banjeremy
  • 301
  • 5
  • 13