Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Article

Recurring Date Generator with Pattern Coding

Rate me:
Please Sign up or sign in to vote.
4.95/5 (51 votes)
4 Sep 2007CPOL4 min read 145.3K   6.9K   98   44
Create recurring dates using a user-defined pattern. Create recurring dates from a coded value that defines what the pattern should be.
Screenshot - ScreenShot01.png

Introduction

I needed a way to create recurring dates using a given pattern of dates such as every weekday, every Saturday and Monday, etc. I couldn't find any sample code on the web other than what you can purchase, so I made this library.

This library not only creates recurring dates but it also returns a coded value that you can store to at a later date, create the same recurring date pattern and adjust the values as needed. This ability to alter the pattern or add more dates to the end of the returned values is what I consider to be a real world requirement. Creating recurring dates is not the issue when creating a pattern of dates, it's being able to store that pattern's design value so it can be used to alter the dates at a later date.

Using the coded pattern value, or Series Info as I call it in this article, you can edit appointment dates similar to those found in Microsoft Outlook. When editing an event in Outlook, you're asked if this is for the current date or the entire series. If you don't have the series date format pattern it's impossible to load up the date controls in the correct format so they match what you first used when you created the recurring dates. This library gives you the ability to do this using the Series Info returned value.

Background

I needed a way to create recurring dates and be able to edit them or adjust the values at a later date.

Using the code

You can open the source solution for a complete sample application that uses the RecurrenceGenerator assembly.

The RecurrenceGenerator library exposes Daily, Weekly, Monthly, and Yearly RecurrenceSettings classes that inherit from RecurrenceSettings class. All the classes have the same constructors.

The constructors allow for:

  1. Start Date (No ending date)
  2. Start Date, End Date (defined ending date)
  3. Start Date, Number of Occurrences (create only x-number of dates)

The constructors are constructed in such a way that you have to, at a minimum, give a Start Date.

C#
/// <summary>
/// Get dates by Start date only. This is for no ending date values.
/// </summary>
/// <param name="startDate"></param>
public DailyRecurrenceSettings(DateTime startDate) : base(startDate) { }

/// <summary>
/// Get dates by Start and End date boundaries.
/// </summary>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
public DailyRecurrenceSettings(DateTime startDate, DateTime endDate) : 
         base(startDate, endDate) { }

/// <summary>
/// Get dates by Start date and number of occurrences.
/// </summary>
/// <param name="startDate"></param>
/// <param name="numberOfOccurrences"></param>
public DailyRecurrenceSettings(DateTime startDate, int numberOfOccurrences) : 
         base(startDate, numberOfOccurrences) { }

The dates are returned in an object called RecurrenceValues. This object is returned by making a call to GetValues([various params based on type of RecurrenceSettings]).

Once the dates are returned, you can use them to list the values such as those in the sample application.

Screenshot - ScreenShot02.png

This workspace shows the dates that were generated by the previous tabs date pattern. The "Series Info" readonly text box shows you the return value that you would store with each date event in your application. Storing this value allows you to create the next date in the pattern as well as generate all the values and adjust them as needed.

Screenshot - ScreenShot05.png

This tab of the sample application allows you to adjust the original Series Info value and create a new set of values. This is the real world in that you may need to extend the date values you're looking at in your calendar control. You might also need to show the next date for the month that's being viewed in your UI control in Winforms.

This third tab allows you to adjust:

  • Start Date
  • Number of occurrences
  • End Date
  • View the definition of the Series Info value. See screen below for example.
Screenshot - ScreenShot06.png

Clicking the "Def." button will bring up a viewer to explain what each Series Info value means. The definitions are different for each pattern (daily, weekly, monthly, yearly).

Screenshot - ScreenShot04.png

This tab shows you the object that can be returned by passing in the Series Info into the RecurrenceHelper shared function GetFriendlySeriesInfo(string seriesInfo).

The Series Info value is different for each date pattern type such as daily, weekly, etc. Each RecurrenceSettings class has a method to view the breakdown of the coded value. This is reached by a call to the RecurrenceHelper.GetPatternDefinition(string seriesInfo).

Points of Interest

Making the sample application helped in the testing process of the date generation. Making a prototype was a good tool to ensure that the RecurrenceGenerator library created the correct values.

History

  • 7 September, 2007: Added the ability to return a modified series of dates using an existing Series Info value and modified Start Date

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BOCA Software Technologies, Inc.
United States United States
.NET Developer in Garner, North Carolina. Specializing in WinForms development in C#, VB.Net.

CEO/Founder BOCA Software Technologies, Inc.

Comments and Discussions

 
GeneralRe: Good coding Pin
Victor Boba24-Oct-08 12:59
Victor Boba24-Oct-08 12:59 
GeneralRe: Good coding Pin
Nagarajan Mohan12-Mar-09 19:02
Nagarajan Mohan12-Mar-09 19:02 
GeneralExcellent!! Pin
Etienne Ngandu18-Feb-08 3:31
Etienne Ngandu18-Feb-08 3:31 
GeneralHelp to Save into Database Pin
sambs6518-Oct-07 22:57
sambs6518-Oct-07 22:57 
GeneralRe: Help to Save into Database Pin
Victor Boba20-Oct-07 7:36
Victor Boba20-Oct-07 7:36 
GeneralBug Pin
Paras Shah19-Sep-07 18:44
Paras Shah19-Sep-07 18:44 
GeneralNothing less than the BEST Pin
Paras Shah15-Sep-07 7:56
Paras Shah15-Sep-07 7:56 
GeneralVery nice Pin
Pete O'Hanlon13-Sep-07 8:49
mvePete O'Hanlon13-Sep-07 8:49 
Generalgreat, but Pin
Laurts13-Sep-07 7:48
Laurts13-Sep-07 7:48 
GeneralRe: great, but Pin
Victor Boba13-Sep-07 9:58
Victor Boba13-Sep-07 9:58 
GeneralRe: great, but Pin
Mark J. Miller10-Sep-09 7:39
Mark J. Miller10-Sep-09 7:39 
GeneralRe: great, but Pin
Victor Boba10-Sep-09 7:50
Victor Boba10-Sep-09 7:50 
QuestionGreat work! Pin
Robert van Vliet7-Sep-07 1:34
Robert van Vliet7-Sep-07 1:34 
AnswerRe: Great work! Pin
Victor Boba7-Sep-07 8:00
Victor Boba7-Sep-07 8:00 
GeneralRe: Great work! Pin
Robert van Vliet7-Sep-07 23:53
Robert van Vliet7-Sep-07 23:53 
GeneralRe: Great work! Pin
Victor Boba10-Sep-07 4:35
Victor Boba10-Sep-07 4:35 
GeneralRe: Great work! Pin
Robert van Vliet10-Sep-07 5:48
Robert van Vliet10-Sep-07 5:48 
GeneralGreat! Pin
merlin9815-Sep-07 5:28
professionalmerlin9815-Sep-07 5:28 
GeneraliCalendar class library Pin
Ryan Morlok4-Sep-07 17:29
Ryan Morlok4-Sep-07 17:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.