| | |
Logic to Convert Days From 1800 to a Date (Month, Day, Year)
![]() |
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
Hi,
This is really a logic problem more so than a stright up C++ problem.
For class I have written a program that takes dates from a text file and then finds the number of days from January 1st 1800 that days is. The program also has to be able to take the number (that number being the number of days since 1800 that date is) and convert it back into a date (in other words finding the month, day, year and day of the week). What makes this quite a bit trickier is taking into account leap years and the effect they have. I can't seem to figure out how to find the date from the number of days. If you want to look at it below is the function that I wrote that finds the number of from January 1st a date is:
If somone could help me in figuring out the logic of how to convert the day number back to a date (and find the day of the week, we do know that January 1st 1800 is a Wednesday) that would be awesome.
thanks,
-Scott
This is really a logic problem more so than a stright up C++ problem.
For class I have written a program that takes dates from a text file and then finds the number of days from January 1st 1800 that days is. The program also has to be able to take the number (that number being the number of days since 1800 that date is) and convert it back into a date (in other words finding the month, day, year and day of the week). What makes this quite a bit trickier is taking into account leap years and the effect they have. I can't seem to figure out how to find the date from the number of days. If you want to look at it below is the function that I wrote that finds the number of from January 1st a date is:
C++ Syntax (Toggle Plain Text)
int DaysFrom1800::numDaysFrom1800 (int day, int month, int year) { // Declare leapCount variable to keep track of number of leap years and days variable // to store number of days int leapCount =0; int days = 0; // For loop to find the number of leap years for (int i=0; i < year; i++) { if (leapCheck(i)) leapCount ++; } // Check to see if current year is a leap year bool thisYear = leapCheck(year); //Find the Number of Days From Complete Years days = ((year-1800-leapCount)*365) + (leapCount*366); // Find the Number of Days From the Months switch (month) { case 1: days = days + day; break; case 2: if (day < 28) days = days + 31 + day; if (thisYear) days = days + 31 + 29; break; case 3: if (thisYear) days = days +31+29+ day; else days = days + 31 + 29 + day; break; case 4: if (thisYear) days = days + 31 + 29 + 31 + day; else days = days + 31 + 28 + 31 + day; break; case 5: if (thisYear) days = days + 31 + 29 + 31 + 30 + day; else days = days +31 + 28 + 31 + 30 + day; break; case 6: if (thisYear) days = days +31 + 29 + 31 + 30 + 31 + day; else days = days + 31 + 28 + 31 + 29 + 31 + day; break; case 7: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + day; break; case 8: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; break; case 9: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day; break; case 10: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day; break; case 11: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day; break; case 12: if (thisYear) days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day; else days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day; break; } return days; }
If somone could help me in figuring out the logic of how to convert the day number back to a date (and find the day of the week, we do know that January 1st 1800 is a Wednesday) that would be awesome.
thanks,
-Scott
Yet another case where Google is your friend.
http://vsg.cape.com/~pbaum/date/date0.htm
There are many good sources of math formulas to do these conversions, and they are much simpler (structurally) than your implementation.
http://vsg.cape.com/~pbaum/date/date0.htm
There are many good sources of math formulas to do these conversions, and they are much simpler (structurally) than your implementation.
How about this one? It's also in the C code snippets. I never knew it would work for anything below 1900, but it shows 01/01/1800 to be a Wednesday.
[php]// function to return the day of the week given the date
// Original Turbo C modified for Pelles C
#include <stdio.h> // for printf(), scanf(), getchar()
int weekday(int month,int day,int year);
char week[7][10] = {
"Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"
};
int main(void)
{
int month, day, year;
printf("Return the day of the week given the date ...\n");
printf("\nEnter date in the form mm/dd/yyyy : ");
scanf("%d/%d/%d",&month,&day,&year);
printf("\nThe day of the week for this date is %s\n\n",week[weekday(month,day,year)]);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// good for 1901 to 2099
//
int weekday(int month,int day,int year)
{
char ix, tx, vx;
switch (month) {
case 2 :
case 6 : vx = 0; break;
case 8 : vx = 4; break;
case 10 : vx = 8; break;
case 9 :
case 12 : vx = 12; break;
case 3 :
case 11 : vx = 16; break;
case 1 :
case 5 : vx = 20; break;
case 4 :
case 7 : vx = 24; break;
}
if (year > 1900)
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
[/php]
[php]// function to return the day of the week given the date
// Original Turbo C modified for Pelles C
#include <stdio.h> // for printf(), scanf(), getchar()
int weekday(int month,int day,int year);
char week[7][10] = {
"Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"
};
int main(void)
{
int month, day, year;
printf("Return the day of the week given the date ...\n");
printf("\nEnter date in the form mm/dd/yyyy : ");
scanf("%d/%d/%d",&month,&day,&year);
printf("\nThe day of the week for this date is %s\n\n",week[weekday(month,day,year)]);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// good for 1901 to 2099
//
int weekday(int month,int day,int year)
{
char ix, tx, vx;
switch (month) {
case 2 :
case 6 : vx = 0; break;
case 8 : vx = 4; break;
case 10 : vx = 8; break;
case 9 :
case 12 : vx = 12; break;
case 3 :
case 11 : vx = 16; break;
case 1 :
case 5 : vx = 20; break;
case 4 :
case 7 : vx = 24; break;
}
if (year > 1900)
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
[/php]
May 'the Google' be with you!
•
•
Join Date: Nov 2004
Posts: 5
Reputation:
Solved Threads: 0
Sorry everyone ...i just found out that leap year is a year which is not only divisible by 4 but also
years divisible by 100 are NOT!
Unless they are also divisible by 400
i never knew about the last 2 points, so now i corrected my program..which gives the result 1/1/1800 as wednesday... Scott, vegaseat's code mighy have helped you....
i will try my best to understand you how to find day of week...
First set a date whose day of week you know..
for instance (21/11/2004 is sunday) then find the no. of days between this date and the date which user entered.then take mod 7..and if its return comes out to be 0 then its sunday else if it is 1 then its monday else if its 2 its tuesday and so on... thats how i did..i get the calendar from the program..
years divisible by 100 are NOT!
Unless they are also divisible by 400
i never knew about the last 2 points, so now i corrected my program..which gives the result 1/1/1800 as wednesday... Scott, vegaseat's code mighy have helped you....
i will try my best to understand you how to find day of week...
First set a date whose day of week you know..
for instance (21/11/2004 is sunday) then find the no. of days between this date and the date which user entered.then take mod 7..and if its return comes out to be 0 then its sunday else if it is 1 then its monday else if its 2 its tuesday and so on... thats how i did..i get the calendar from the program..
The number of days from 01/01/1800 to 11/21/2004 is 74833. I used this little code below. Interesting as far as algorithms are concerned, but the span needs to be more than a year to trap leap years correctly.
[php]// Find the number of days between given dates mm/dd/yyyy
// algorithm does not work properly with leap years, if the
// span is less than a year
// right number for 01/01/2004 to 01/01/2005 (366 days)
// wrong number for 01/01/2004 to 12/31/2004 (364 days)
// right number for 01/01/1900 to 01/01/2000 (36524 days)
// right number for 01/01/2000 to 01/01/2100 (36525 days)
// since 2000 was a leap year and 1900 was not
// (years ending with 00 have to be divisible by 400 to leap)
// Code modified to compile with Pelles C (dns)
#include <stdio.h> // printf(), scanf()
long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);
int main(void)
{
int sm, sd, sy; // starting date
int em, ed ,ey;
long old;
printf("\n Calculate days between two dates ...");
printf("\n\n Enter starting date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&sm,&sd,&sy);
printf("\n Enter ending date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&em,&ed,&ey);
old = days(sm,sd,sy,em,ed,ey);
printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
old,sm,sd,sy,em,ed,ey);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
//
// returns number of days between start and end dates
// span has to be more than a year to catch leap years properly
//
long days(int sm, int sd, int sy, int em, int ed, int ey)
{
long fac1, fac2, elap;
fac1 = factor(sm, sd, sy);
fac2 = factor(em, ed, ey);
elap = fac2 - fac1;
return (elap);
}
long factor(int mm, int dd, int yy)
{
long xx;
xx = 365 * yy + dd + 31 * (mm - 1);
if (mm < 3)
xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
else
xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
return (xx);
}
[/php]
[php]// Find the number of days between given dates mm/dd/yyyy
// algorithm does not work properly with leap years, if the
// span is less than a year
// right number for 01/01/2004 to 01/01/2005 (366 days)
// wrong number for 01/01/2004 to 12/31/2004 (364 days)
// right number for 01/01/1900 to 01/01/2000 (36524 days)
// right number for 01/01/2000 to 01/01/2100 (36525 days)
// since 2000 was a leap year and 1900 was not
// (years ending with 00 have to be divisible by 400 to leap)
// Code modified to compile with Pelles C (dns)
#include <stdio.h> // printf(), scanf()
long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);
int main(void)
{
int sm, sd, sy; // starting date
int em, ed ,ey;
long old;
printf("\n Calculate days between two dates ...");
printf("\n\n Enter starting date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&sm,&sd,&sy);
printf("\n Enter ending date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&em,&ed,&ey);
old = days(sm,sd,sy,em,ed,ey);
printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
old,sm,sd,sy,em,ed,ey);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
//
// returns number of days between start and end dates
// span has to be more than a year to catch leap years properly
//
long days(int sm, int sd, int sy, int em, int ed, int ey)
{
long fac1, fac2, elap;
fac1 = factor(sm, sd, sy);
fac2 = factor(em, ed, ey);
elap = fac2 - fac1;
return (elap);
}
long factor(int mm, int dd, int yy)
{
long xx;
xx = 365 * yy + dd + 31 * (mm - 1);
if (mm < 3)
xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
else
xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
return (xx);
}
[/php]
May 'the Google' be with you!
![]() |
Similar Threads
- convert int to string (C++)
- Day Of Year Problem (C++)
Other Threads in the C++ Forum
- Previous Thread: CGI with C++. my cgi script in perl works perfectly but not c++
- Next Thread: Help with Class, stuck.
| Thread Tools | Search this Thread |
api array based binary bitmap build c++ c++intmain() c/c++ char class classes client code coding compile console conversion count counttheoccurenceofanintegerinthe10inputs delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption environment error file forms fstream function functions game givemetehcodez graph gui homeworkassignment homeworkhelp homeworkhelper i/o iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple multipledimensionarray news node number numbers numbertoword output parameter pointer problem program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






