javascriptrrule

rrule js issues with RRuleSet


I'm using rrule js libary and Its seems like the RRuleSet not invoke the toText() correctly.

const rruleSet = new RRuleSet();

// Add a rrule to rruleSet
rruleSet.rrule(
  new RRule({
    freq: RRule.DAILY,
    interval: 1,
    byweekday: [RRule.MO],
    dtstart: new Date('2023-07-17T08:00:00.000Z'),
    until: new Date('2023-07-01T08:00:00.000Z'),
  })
);

rruleSet.exdate(new Date('2023-07-24T08:00:00.000Z"));

//results:

  console.log(rruleSet.toText()); //2023 every year ===> WHAT?!

  console.log(rruleSet.toString());//DTSTART:20230718T171738Z
  //RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=MO;UNTIL=20230801T000000Z
  //EXDATE:20230724T000000Z

  console.log(rrulestr(rruleSet.toString()).toText()); // 2023 every year ===> again ,WHAT?!,

the function toText() now working as excepted. "every day on Monday until August 1 except 2023-07-24 "


Solution

  • Rules are timestamps precise to the second, not just dates. You need to exclude the exact timestamp that's going to be generated, not just the day it would occur on. If you're only interested in days, set the time to a fixed value, like midnight:

    const { RRule, RRuleSet } = rrule;
    
    const rruleSet = new RRuleSet();
    
    rruleSet.rrule(
      new RRule({
        freq: RRule.DAILY,
        interval: 1,
        byweekday: [RRule.MO],
        dtstart: new Date("2023-07-18T00:00:00Z"),
        until: new Date("2023-08-01T00:00:00Z"),
      })
    );
    
    rruleSet.exdate(new Date("2023-07-24T00:00:00Z"));
    
    console.log(rruleSet.all());
    <script src="https://jakubroztocil.github.io/rrule/dist/es5/rrule.min.js"></script>

    Why the textualisation just says "every year"…? Meh… because the library isn't clever enough to turn complex rulesets into strings? If in doubt, you can turn every individual rule in the set into a string, but that of course won't include the exclusions. Which is a tricky problem, which is probably why the library doesn't tackle it (correctly).