Articles in this section
Category / Section

How to parse the Recurrence Rule (RRule) in server side?

4 mins read

This article explains the way to parse the recurrence rule (RRule) at server side using RecurrenceHelper utility class to retrieve start date, end date, recurring dates and etc.

View RecurrenceHelper Utility Class in GitHub

 

RecurrenceHelper utility class provides various methods to parse RRule. For example, GetRecurrenceDateTimeCollection method returns the date collection of occurrences based on RRule. It exposes below methods with different parameters to handle difference cases,

  1. RRule – Recurrence rule to parse and retrieve occurrences.
  2. RecStartDate – Start date to parse RRule. Without start date, recurrence rule can’t be parsed to retrieve occurrences.
  3. RecException (Optional) – To pass recurrence exception dates.
  4. NeverCount (Optional) – Maximum no of occurrences to be returned when RRule is defined with No End Date.

Below example shows retrieving dates collection using GetRecurrenceDateTimeCollection method by passing RRule and start date.

var dateCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection("FREQ = WEEKLY; BYDAY = FR,SA; INTERVAL = 1;", DateTime.Now);

 

Below example shows setting the maximum occurrences count (ex: 60) to be retrieved when “No End Date” option is present in the rrule.

var dateCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection("FREQ = WEEKLY; BYDAY = FR,SA; INTERVAL = 1;", DateTime.Now, 60);

 

Below example shows passing recurrence exception date (ex: "20180610T040000Z") for excluding the dates.

var dateCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection("FREQ = WEEKLY; BYDAY = FR,SA; INTERVAL = 1;", DateTime.Now, "20180610T040000Z,20180602T040000Z", 60);

 

The following steps explains the way to parse the Recurrence Editor control’s recurrence rule using RecurrenceHelper utility class.

 

Step 1: Create an ASP.NET Core application with Recurrence Editor by referring this link.

 

Also, define the change client-side event as shown in the following code example.

<ejs-recurrenceeditor id="RecurrenceEditor" change="onChange"></ejs-recurrenceeditor>

 

Step 2: Within the Recurrence Editor change event, the selected recurrence rule is sent to the getDates controller function as shown in the following code example.

    function onChange(args) {
        var recurrenceObj = document.getElementById("RecurrenceEditor").ej2_instances[0];
        var dates = recurrenceObj.getRecurrenceDates(new Date(), args.value); // Here the first argument should be the start date and second argument is the recurrence rule
        $.ajax({
            url: '@Url.Action("getDates", "Home")',
            data: { 'dates': JSON.stringify(args.value)},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                $("#Collection").html("");
                var object = [];
                for (var i = 0; i < result.length; i++) {
                    $("#Collection").append('<div id="date_' + i + '">' + result[i] + '</div>');
                }
            }
        });
     }

 

Step 3: In getDates controller function recurrence rule is parsed using GetRecurrenceDateTimeCollection method and date collections are returned as shown in the following code example.

        public JsonResult getDates(string dates)
        {
            var recurrenceRule = JsonConvert.DeserializeObject<string>(dates);
            var dateCollection = RecurrenceHelper.GetRecurrenceDateTimeCollection(recurrenceRule, DateTime.Now);
            return Json(dateCollection);
        }

 

Step 4: Run the sample, initially default Recurrence Editor will display. When the “Repeat” rule is changed, it’s corresponding date collections will return.

 

View Sample in GitHub

 

Refer to our documentation and online samples for more features. If you have any queries, please let us know in the comments below. You can also contact us through our Support forum or Support ticket. We are happy to assist you!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied