Search code examples
dartrrule

In Dart get next occurring date from an rrule


Is there a Dart library or, lacking one, a correct way to handle finding the next date that would be returned from a correctly formatted rrule?

For example give the following rule: "FREQ=WEEKLY;BYDAY=WE;INTERVAL=1;DTSTART=20180328T160000Z"

I would want a date time value of the next occurring Wed.


Solution

  • Don't know if there is a library for this, but I quickly came up with this. Not complete, but it's a start.

    void main() {
        var today = DateTime.parse('20180328T160000Z');
    
        var days = today.weekday < DateTime.wednesday ? DateTime.wednesday - today.weekday : 7 - today.weekday + DateTime.wednesday;
    
        var next = today.add(new Duration(days:days));
        print(next);
    }
    

    Others might come up with something better. Parsing the rule would be the hard part.