Day of the Week
-
Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob
-
Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob
Are you using MFC?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob
Something like the following, with maybe a tweak or three:
struct tm time_in = { 0, 0, 0, 1, 7, 2013 - 1900 }; // August 1st, 2013
time_t time_out = mktime(&time_in);
struct tm *time_local = localtime(&time_out);
_tprintf(_T("%u\n"), time_local->tm_wday);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob
-
Nice idea, but the OP was looking for something portable.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Given a month and year, I want to write C++ code to determine what day of the week the month starts at. For example, for 2/2013, I want the code to return Friday (or an integer representing Friday) because February 1, 2013 was a Friday. The code needs to be portable. What is the best way to do this? Thanks Bob
I would suggest converting the date to a Julian Day Number. Here is a function we use to convert day, month and year to such a number. The input values (D, M, Y) are all unsigned int.
unsigned int yh = Y;
unsigned long c, ya, j;
if (M > 2)
{
M -= 3; }else{
M += 9;
Y--;
}
c = Y / 100;
ya = Y - 100*c;
j = ((146097*c)>>2) + ((1461*ya)>>2) + (153*M + 2)/5 + D;Using that you just need the Julian Day Number corresponding to a date for which you know the day of week as a reference. You can then subtract the numbers to get the days in between. Please note that the function is only valid from 14th September 1752 (when the Gregorian calendar started).
The good thing about pessimism is, that you are always either right or pleasently surprised.