Thursday, May 25, 2006

Calendar Object in C Sharp.NET

A friend of mine asked me to create a calendar object in C Sharp. I thought it might be useful to others too

So here is a one for you



using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithms
{
public class Calendar
{
private DateTime currentDate;
private int currentDay;
private int currentDayWeek;
private int currentMonth;
private int currentYear;
private bool leapYear;

public Calendar()
{
DateTime cDate = DateTime.Now;
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;
}
public Calendar(DateTime cDate)
{
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;

if (currentYear % 400==0)
leapYear = true;
else if (currentYear % 100==0)
leapYear = false;
else if (currentYear % 4==0)
leapYear = true;
else
leapYear = false;

}
public DateTime CurrentDate
{
get { return currentDate; }
set { currentDate = value;}
}
public int CurrentDay
{
get { return currentDay; }
set { currentDay = value; }
}
public int CurrentDayWeek
{
get { return currentDayWeek; }
set { currentDayWeek = value; }
}
public int CurrentMonth
{
get { return currentMonth; }
set { currentMonth = value; }
}
public int CurrentYear
{
get { return currentYear; }
set { currentYear= value; }
}
public bool LeapYear
{
get { return leapYear; }
}

public void Display()
{
Console.Write("== Calendar ==\n");

DateTime firstDay = currentDate.AddDays(1 - currentDay);
string dayDisplay ="";

if (Convert.ToInt32(firstDay.DayOfWeek) == 0)
{
dayDisplay = "Mo Tu We Th Fr Sa Su";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 1)
{
dayDisplay = "Tu We Th Fr Sa Su Mo";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 2)
{
dayDisplay = "We Th Fr Sa Su Mo Tu";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 3)
{
dayDisplay = "Th Fr Sa Su Mo Tu We";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 4)
{
dayDisplay = "Fr Sa Su Mo Tu We Th";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 5)
{
dayDisplay = "Sa Su Mo Tu We Th Fr";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 6)
{
dayDisplay = "Su Mo Tu We Th Fr Sa";
}

Console.Write(dayDisplay + "\n");

// Common content
Console.Write("01 02 03 04 05 06 07\n");
Console.Write("08 09 10 11 12 13 14\n");
Console.Write("15 16 17 18 19 20 21\n");
Console.Write("22 23 24 25 26 27 28\n");

//Variable Date

// Feb 2
if (currentMonth == 2)
{
if (leapYear)// Feb 2 leap year
Console.Write("29 00 00 00 00 00 00\n");
else
Console.Write("00 00 00 00 00 00 00\n");
}

if ((currentMonth == 4) || (currentMonth == 6) || (currentMonth == 9) || (currentMonth == 11))
{
// 4, 6, 9, 11
Console.Write("29 30 00 00 00 00 00\n");
}
if ((currentMonth == 1) || (currentMonth == 3) || (currentMonth == 5) || (currentMonth == 7) || (currentMonth == 8) || (currentMonth == 10) || (currentMonth == 12))
{
// 1,3,5,7,8,10,12
Console.Write("29 30 31 00 00 00 00\n");
}



}

}
}