Time and date functions in c#
I have been used to work with the COleDateTime objects in Visual Studio 98. There is a similar solution in C#?. Yes. The DateTime and TimeSpan Object. There are very similar to the useful COleDateTime and COleDateTimeSpan objects.
Here is a sample program that I have made to test the zeller´s congruence algorithm to determine the day of week from the Date. I have tested it from now until the 2100.
/* Zeller's Congruence to determine day of week */
int get_dayofweek(int date)
{
int d = date % 100;
int m = (date % 10000) / 100;
int y = date / 10000;
if (m < 3)
{
m = m + 12 ;
y = y - 1;
}
return ((2 + d + (13*m-2)/5 + y + y/4 - y/100 + y/400) % 7);
}
private void TestDayOfWeekAlgo()
{
DateTime t=new DateTime(2005,1,1,12,0,0);
TimeSpan i=new TimeSpan(1,0,0,0);
do
{
t+=i;
if ((int)t.DayOfWeek!=get_dayofweek(t.Year*10000+t.Month*100+t.Day))
{
MessageBox.Show("The day of week algorithm is not working correctly","Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
} while(t.Year<2100);
}
If you have time, you can read the ISO 8601 document about standard dates, and also some interesting things as about how to number the differents weeks of the year.
Here is a sample program that I have made to test the zeller´s congruence algorithm to determine the day of week from the Date. I have tested it from now until the 2100.
/* Zeller's Congruence to determine day of week */
int get_dayofweek(int date)
{
int d = date % 100;
int m = (date % 10000) / 100;
int y = date / 10000;
if (m < 3)
{
m = m + 12 ;
y = y - 1;
}
return ((2 + d + (13*m-2)/5 + y + y/4 - y/100 + y/400) % 7);
}
private void TestDayOfWeekAlgo()
{
DateTime t=new DateTime(2005,1,1,12,0,0);
TimeSpan i=new TimeSpan(1,0,0,0);
do
{
t+=i;
if ((int)t.DayOfWeek!=get_dayofweek(t.Year*10000+t.Month*100+t.Day))
{
MessageBox.Show("The day of week algorithm is not working correctly","Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
} while(t.Year<2100);
}
If you have time, you can read the ISO 8601 document about standard dates, and also some interesting things as about how to number the differents weeks of the year.
