Wednesday, April 18, 2007

4. Finding the Weekday of a Given Date



Is April 18, 2007 a Wednesday?
This program asks the user to enter a random date, like 4/18/2007.
Then it will tell you the weekday, like Wednesday.



I am encouraging the readers of this title to refer to this link first. It is the source for my implementation. It is a more general approach rather than a simple calculation.

Program.cs

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

namespace DayFinder
{
class Program
{
static void Main(string[] args)
{
Date D = new Date(); // Don't need my coding to define the constructor.
Console.Write("Enter month: ");
D.month = uint.Parse(Console.ReadLine());
Console.Write("Enter day: ");
D.day = uint.Parse(Console.ReadLine());
Console.Write("Enter year (yyyy): ");
D.year = uint.Parse(Console.ReadLine());

Console.WriteLine("You have entered: "+D.month+"/"+D.day+"/"+D.year);

//Assuming the entries are legal:
/*********************************************
For July 13, 1989:
1. Divide the last two digits of the year by 12. 89 divided by 12 is 7, with 5 left over.

2. Find how many 4's go into the remainder evenly. 5 divided by 4 is 1, with a remainder that we ignore.
3. Add these three numbers. 7 + 5 + 1 = 13.
4. Divide the sum by 7 and take the remainder. 13 divided by 7 is 1, with a remainder of 6.
5. Add the remainder to the century's anchor day to find the year's Doomsday. Wendesday + 6 = Tuesday.
6. Use the month's Doomsday to find the day you need. July's Doomsday is 7/11, a Tuesday, so July 13 is a Thursday.
Source: http://www.theworldofstuff.com/other/day.html
*********************************************************/

uint yearTwoDigits = D.year % 100;
uint ydivision = yearTwoDigits / 12;
uint yremainder = yearTwoDigits % 12;
uint NumberOfFours = yremainder / 4;
uint FinalRemainder = (ydivision + yremainder + NumberOfFours) % 7;
uint AnchorDay = UtilityFunctions.GetAnchorDay(((uint) (D.year/100) * 100) % 400);

uint YearsDoomsDay = (FinalRemainder + AnchorDay) % 7;
int difference = (int) D.day - (int) UtilityFunctions.GetMonthsDoomsDay(D.month, D.year);
int ExactDay = (int) YearsDoomsDay + difference; //Need typecasting
if (ExactDay <0) face="courier new">ExactDay += 7;
else if (ExactDay > 7)
ExactDay=ExactDay % 7;

string WeekDay=UtilityFunctions.GetExactDay(ExactDay);

Console.WriteLine("This date is a "+WeekDay);
Console.Read();
// Logic Errors: 2/29/2007 => Thursday. There is no such 2/29 in 2007!
// Day undefined for 7/3/1989!
}
}
}


UtilityFunc.cs

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

namespace DayFinder
{
class Date
{
public uint month; //public is needed!
public uint day;
public uint year;
}


class UtilityFunctions
{

public static uint GetAnchorDay(uint AnchorCheck)
{
uint AnchorDay= 0;
switch (AnchorCheck)
{
case 100:
AnchorDay = 0; //"Sunday"
break;
case 200:
AnchorDay = 5; //"Friday"
break;
case 300:
AnchorDay = 3; //"Wednesday"
break;
case 0:
AnchorDay = 2; //"Tuesday"
break;
}
return AnchorDay;
}
public static int GetMonthsDoomsDay(uint Month, uint Year)
{
int DoomsDay = 0;
switch (Month)
{
case 1: //January
DoomsDay = 3 + (int)isLeap(Year);
break;
case 2: //February
DoomsDay = 28;
break;
case 3: //...
DoomsDay = 0;
break;
case 4:
DoomsDay = 4;
break;
case 5:
DoomsDay = 9;
break;
case 6:
DoomsDay = 6;
break;
case 7:
DoomsDay = 11;
break;
case 8:
DoomsDay = 8;
break;
case 9:
DoomsDay = 5;
break;
case 10:
DoomsDay = 10;
break;
case 11:
DoomsDay = 7;
break;
case 12:
DoomsDay = 12;
break;
}
return DoomsDay;
}
internal static uint isLeap(uint Year)
{
if ((Year % 4) == 0) return 1;
else return 0;
}
public static string GetExactDay(int ExactDay)
{
string WeekDay=null;
switch (ExactDay)
{
case 0:
WeekDay = "Sunday";
break;
case 1:
WeekDay = "Monday";
break;
case 2:
WeekDay = "Tuesday";
break;
case 3:
WeekDay = "Wednesday";
break;
case 4:
WeekDay = "Thursday";
break;
case 5:
WeekDay = "Friday";
break;
case 6:
WeekDay = "Saturday";
break;
}
return WeekDay;
}

}
}

1 comment:

dotneteasy said...

Discussion about Exceptions:
Notice these lines in Program.cs:

if (ExactDay <0)
ExactDay += 7;
else if (ExactDay > 7)
ExactDay=ExactDay % 7;

-ExactDay is the day, the program will tell me, like 1 for Monday or 5 for Friday.
-It might be negative or positive for some reason.
-The if statements are adjusting them to be in the range of 0 to 7.
-The else part works perfectly.
-However, the
if (ExactDay <0)
ExactDay += 7;
statement might still produce negative results.
-I did not correct this problem just for one reason: TO LEARN HOW THE EXCEPTIONS WORK.
-Now, UtilityFunctions.GetExactDay(ExactDay) returns the answer of the program, like I said, Monday for ExactDay=1, Friday for ExactDay=5.
-So, what is the line of code to be embedded into GetExactDay() method to handle this exception? Thanks.