Few random development Tips-N-Tricks-8

A simple technique to generate weekly recurring dates in C#.

img1

aspx

<form id="form1" runat="server">
<div>
<h1>Weekly Recurring Date Generation</h1>
<b>Choose Date:</b>&nbsp;<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar><br />
Generate Dates For every <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> weeks.
<br />
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4">
    <asp:ListItem Value="1" Text="Monday"></asp:ListItem>
    <asp:ListItem Value="2" Text="Tuesday"></asp:ListItem>
    <asp:ListItem Value="3" Text="Wednesday"></asp:ListItem>
    <asp:ListItem Value="4" Text="Thursday"></asp:ListItem>
    <asp:ListItem Value="5" Text="Friday"></asp:ListItem>
    <asp:ListItem Value="6" Text="Saturday"></asp:ListItem>
    <asp:ListItem Value="7" Text="Sunday"></asp:ListItem>
    </asp:CheckBoxList>
<br />
End after <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> occurances.
<br />   
<asp:button ID="Button1" runat="server" text="Generate Occurances" 
        onclick="Button1_Click" />
<br /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>

Codebehind.

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "";
    DateTime today = Calendar1.SelectedDate;
    int _Interval = int.Parse(TextBox1.Text);
    for (int r = 0; r < int.Parse(TextBox2.Text); r++)
    {
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Selected)
            {
                GenerateOccurances(today, _Interval, int.Parse(li.Value));
            }
        }
        //Change the week for next interval
        today = today.AddDays(7 * _Interval);
        Label1.Text += "<br>***************************";
    }
}
private void GenerateOccurances(DateTime today, int _interval, int CurrdayOfWeek)
{
    int n1 = -1;
    if (today.DayOfWeek == DayOfWeek.Monday)
    {
        n1 = 1;
    }
    if (today.DayOfWeek == DayOfWeek.Tuesday)
    {
        n1 = 2;
    }
    if (today.DayOfWeek == DayOfWeek.Wednesday)
    {
        n1 = 3;
    }
    if (today.DayOfWeek == DayOfWeek.Thursday)
    {
        n1 = 4;
    }
    if (today.DayOfWeek == DayOfWeek.Friday)
    {
        n1 = 5;
    }
    if (today.DayOfWeek == DayOfWeek.Saturday)
    {
        n1 = 6;
    }
    if (today.DayOfWeek == DayOfWeek.Sunday)
    {
        n1 = 7;
    }
    //Add weeks
    DateTime CurrDate = today.AddDays(7*_interval);
    int DayDiff = n1 - CurrdayOfWeek;
    if (DayDiff != 0)
    {
        DateTime FutureDate = CurrDate.AddDays(-DayDiff);
        Label1.Text += "<br> Future Date:&nbsp;&nbsp;&nbsp;" + FutureDate.DayOfWeek + ", " + FutureDate.ToString();
    }
    else
    {
        DateTime FutureDate = CurrDate;
        Label1.Text += "<br> Future Date:&nbsp;&nbsp;&nbsp;" + FutureDate.DayOfWeek + ", " + FutureDate.ToString();
    }
}

Thanks for reading.

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s