Articles in this section
Category / Section

How to parse the RecurrenceRule at server side?

2 mins read

This knowledge base article explains the way to parse the recurrence rule at server side by making use of a new generic utility class RecurrenceHelper.

 

By referring to the RecurrenceHelper class in your sample project, you can make use of the GetRecurrenceDateTimeCollection method which automatically generates the date instances as a result. Below 4 type parameters can be passed to GetRecurrenceDateTimeCollection method.

 

Type 1: Recurrence rule and start date value (Mandatory parameters).

In this type, we need to pass two mandatory arguments such as recurrence rule and recurrence start date. 

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

 

Type 2: Maximum number of occurrences to retrieve. 

In this type, we can pass the maximum occurrences count (ex: 60) to be retrieved when “Ends Never” option is present in the recurrence. By default, the Ends never maximum count is processed as 43.

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

 

Type 3: RecurrenceException.

In this type, we can pass the RecurrenceException date (ex: "20180610T040000Z") for excluding the dates along with mandatory arguments.

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

 

Type 4: Maximum number of occurrences to retrieve and RecurrenceException.  

 

In this type, we can pass the RecurrenceException for excluding the dates (ex: "20180610T040000Z") and the maximum occurrence count (ex: 60) for the “Ends Never” option, with the mandatory arguments.  

 

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 MVC application with Recurrence Editor by referring the following user guide link.

https://ej2.syncfusion.com/aspnetmvc/documentation/schedule/recurrence-editor/?no-cache=1#recurrence-editor

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

@Html.EJS().RecurrenceEditor("RecurrenceEditor").Change("onChange").Render()

 

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); 
    $.ajax({
        url: '@Url.Action("getDates", "Home")',
        data: { 'dates': JSON.stringify(args.value)},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            console.log(result);
            $("#Collection").html("");
            var object = [];
            for (var i = 0; i < result.length; i++) {
                object.push(new Date(result[i].match(/\d+/)[0] * 1));
            }
            for (var i = 0; i < object.length; i++) {                    
                $("#Collection").append('<div id="date_' + i + '">' + object[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, JsonRequestBehavior.AllowGet);
}

 

 

Step 4: Run the sample, initially Recurrence Editor is displayed as shown in the following image.

 

      Figure 1: Default Recurrence Editor.

 

When the rule is changed to Daily, corresponding date collections will be displayed as shown in the following image.

 

     Figure 2: Recurrence Editor with Daily rule and its corresponding date collections.

 

Please refer the example and recurrence helper class file from the following GitHub links.

Example - Recurrence Parser

Class file - Recurrence Helper

 

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