.Net Calendar Week Selection

by Matt 17. April 2009 15:43

I had to make a widget that displays a week of a calendar from Sunday on the left to Saturday on the right.  The program it's to be implemented in passes DateTime.ToString objects as a url parameter so I was stuck with having to do that.  Here is the little web forms project I whipped up to implement into the main app:

First, the class has three private members:

private DateTime WeekFirstDate;
private string UrlToday;
private DateTime Today;

The Page_Load grabs the date from the querystring and tries to parse it.  If it is empty or invalid, we use the current date.

protected voidPage_Load(objectsender, EventArgs e)
{
    UrlToday= Request.QueryString["fldate"];

    bool success = DateTime.TryParse(UrlToday, outToday);
    if(!success)
    {   //if time wasnt passed in or conversion to DateTime wasn't successful, use today
       Today = DateTime.Now;
    }

    WeekFirstDate = Today.AddDays(-6);

    int i = GetSunday();
    SetWeekDays(i);
    HighlightSelectedDate();

    Back.Text = "<a href='Default.aspx?fldate="+ Today.AddDays(-7) + "'>Previous"+ "</a>";
    Forward.Text = "<a href='Default.aspx?fldate="+ Today.AddDays(7) + "'>Forward"+ "</a>";
}

 

The functions it calls are as follows.  First, we figure out the Sunday in that particular week:

public int GetSunday()
{
    int i = 0;
    for (i = 0; i < 7; i++)
    {
        Sunday.Text = WeekFirstDate.AddDays(i).ToString("dddd, dd MMMM");

        if (Sunday.Text.Contains("Sunday"))
        {
            break;
        }
    }
    return i;
}  

 

Then we set the .Text property of the 7 literals on our Default.aspx page:

public voidSetWeekDays(int i)
{
    Monday.Text = WeekFirstDate.AddDays(i+1).ToString("dddd, dd MMMM");
    Tuesday.Text = WeekFirstDate.AddDays(i + 2).ToString("dddd, dd MMMM");
    Wednesday.Text = WeekFirstDate.AddDays(i + 3).ToString("dddd, dd MMMM");
    Thursday.Text = WeekFirstDate.AddDays(i + 4).ToString("dddd, dd MMMM");
    Friday.Text = WeekFirstDate.AddDays(i + 5).ToString("dddd, dd MMMM");
    Saturday.Text = WeekFirstDate.AddDays(i + 6).ToString("dddd, dd MMMM");
}

 

And that’s it!  Now to make the literals links that pass the DateTime.ToString() through the url and highlight the currently selected date:

public void HighlightSelectedDate()
{
    IList<Literal> literals = new List<Literal>();
    literals.Add(Sunday);
    literals.Add(Monday);
    literals.Add(Tuesday);
    literals.Add(Wednesday);
    literals.Add(Thursday);
    literals.Add(Friday);
    literals.Add(Saturday);

    foreach (Literal l in literals)
    {
        if (l.Text == Today.ToString("dddd, dd MMMM"))
        {
            l.Text = "<b>" + l.Text + "</b>";
        }

        string substring = l.Text.Substring(3, l.Text.Length - 7);
        if (l.Text.Contains("<b>"))
        {
            l.Text = "<a href='Default.aspx?fldate=" +
                     Convert.ToDateTime(substring) + "'>" + l.Text + "</a>";
        }
        else
        {
            l.Text = l.Text = "<a href='Default.aspx?fldate=" +
                     Convert.ToDateTime(l.Text) + "'>" + l.Text + "</a>";
        }
    }
}

 

What we have is a widget that will always display Sunday first, always default to the current week, and allows users to skip forward and backward a week at a time.

Categories: ASP.NET | C#