-
Is there a funktion ?
Hello,
Im a Rookie and maybe you can/ want to help me out.
Is there a funktion that can convert a date to the day ?
I want the user to put in a date and the programm must give
the day and if possible the week number.
Im programming a console application. So I,m looking for a funktion in c++ standard libary.
Thanks in advandge.
And sorry for the bad englisch.
-
I think you'd need to create some kind of calendar... something like
Code:
char January[31];
January[0] = "Monday";
January[1] = "Tuesday";
// ...
But I'm not sure about the rest.
-
not that I know of... since it seems as if english isn't your first language, your probably not using US date format... so your format would be DD/MM/YYYY... just read it in like this:
Code:
...
int day;
int month;
int year;
char null;
cin>>day>>null>>month>>null>>year;
...
then you cold set an array like this:
Code:
...
int calander[YEAR-2004][MONTH NUMBER-1][DAYS-1];
...
then run a loop through putting a number representation of the day of the week, as in monday=1,tuesday=2, etc. I would put a limit on it, like no dates before last year or something... and no dates after 2010
-
There is nothing standard to c++ that will automatically calculate the day of the week for you, however you can calculate it mathematically yourself. Take the following into consideration:
1996 = MON *
1997 = WED
1998 = THURS
1999 = FRI
2000 = SAT *
2001 = MON
2002 = TUES
2003 = WED
2004 = THURS *
2005 = SAT
2006 = SUN
2007 = MON
2008 = TUES *
2009 = THURS
2010 = FRI
2011 = SAT
2012 = SUN *
(*=leap year)
January 1 of year zero was a FRIDAY. Each January 1 the year after a leap year is two days of the week later and each year after that up to and including the next leap year is one day later.
I just figured this out by looking at the day of the year each year and finding the pattern.
Once you have the day of the week of the first day of the year, all you have to do is do the math to calculate the day of the week within that year. For example, january 30, 1996 would be done like so:
1/1/1996 = MONDAY
30 - 1 = 29 days later
29 % 7 days a week = 1 day of the week later than 1/1/1996
therefore, 1/30/1996 = TUESDAY
-
Thank you all for this reply.
greetings Gerard.
-
-
Hi The following is a function for finding day of week I used in a college assignment
In the main program you could use
cout<<dateName(18,11,2003);
or input a date and parse into day, month, year
Code:
//Function to find day name of date
string dateName(int day, int month, int year)
{//start of function dateName
string dname[7]={"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
int Y,M,D,d;//variables local to function
// array of day names 0 to 6
if (month < 3)
{//start of if
Y = year - 1;
M = month + 12;
D = day;
}//end of if
else
{ //start of else
Y = year;
M = month;
D = day;
} //end of else
//formulae for finding day of week eg 0 to 6
d = ((13 * M + 3) / 5 + D + Y + Y / 4 - Y / 100 + Y / 400 + 1) %7;
// d = 0 to 6
return dname[d];
}//end of function dateName
Regards William