Articles in this section
Category / Section

How to parse the recurrence rule at server side?

4 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 in ASP.NET MVC Schedule.

 

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

 

Conclusion

I hope you enjoyed learning about how to parse the recurrence rule at server side.

You can refer to our ASP.NET MVC Schedule feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Schedule example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always 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