Logic to Convert Days From 1800 to a Date (Month, Day, Year)

Reply

Join Date: Oct 2004
Posts: 10
Reputation: skamen is an unknown quantity at this point 
Solved Threads: 0
skamen skamen is offline Offline
Newbie Poster

Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #1
Nov 17th, 2004
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:

  1. int DaysFrom1800::numDaysFrom1800 (int day, int month, int year) {
  2. // Declare leapCount variable to keep track of number of leap years and days variable
  3. //    to store number of days
  4. int leapCount =0;
  5. int days = 0;
  6.  
  7. // For loop to find the number of leap years
  8. for (int i=0; i < year; i++) {
  9.        if (leapCheck(i))
  10.            leapCount ++;
  11.    }
  12. // Check to see if current year is a leap year
  13. bool thisYear = leapCheck(year);
  14.  
  15. //Find the Number of Days From Complete Years
  16. days = ((year-1800-leapCount)*365) + (leapCount*366);
  17.  
  18. // Find the Number of Days From the Months
  19. switch (month) {
  20. case 1:
  21.    days = days + day;
  22.    break;
  23. case 2:
  24.    if (day < 28)
  25.        days = days + 31 + day;
  26.        if (thisYear)
  27.            days = days + 31 + 29;
  28.        break;
  29. case 3:
  30.    if (thisYear)
  31.        days = days +31+29+ day;
  32.        else
  33.        days = days + 31 + 29 + day;
  34.        break;
  35. case 4:
  36.    if (thisYear)
  37.        days = days + 31 + 29 + 31 + day;
  38.        else
  39.        days = days + 31 + 28 + 31 + day;
  40.        break;
  41. case 5:
  42.    if (thisYear)
  43.        days = days + 31 + 29 + 31 + 30 + day;
  44.    else
  45.        days = days +31 + 28 + 31 + 30 + day;
  46.    break;
  47. case 6:
  48.    if (thisYear)
  49.        days = days +31 + 29 + 31 + 30 + 31 + day;
  50.    else
  51.        days = days + 31 + 28 + 31 + 29 + 31 + day;
  52.    break;
  53. case 7:
  54.    if (thisYear)
  55.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + day;
  56.    else
  57.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + day;
  58.    break;
  59. case 8:
  60.    if (thisYear)
  61.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
  62.    else
  63.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
  64.    break;
  65. case 9:
  66.    if (thisYear)
  67.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + day;
  68.    else
  69.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + day;
  70.    break;
  71. case 10:
  72.    if (thisYear)
  73.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
  74.    else
  75.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
  76.    break;
  77. case 11:
  78.    if (thisYear)
  79.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
  80.    else
  81.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + day;
  82.    break;
  83. case 12:
  84.    if (thisYear)
  85.        days = days + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
  86.    else
  87.        days = days + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + day;
  88.    break;
  89. }
  90.  
  91. return days;
  92.  
  93. }

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #2
Nov 17th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #3
Nov 19th, 2004
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: Neo99 is an unknown quantity at this point 
Solved Threads: 0
Neo99 Neo99 is offline Offline
Newbie Poster

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #4
Nov 21st, 2004
January 1st 1800 is Monday not Wednesday..please check if i am right
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: Neo99 is an unknown quantity at this point 
Solved Threads: 0
Neo99 Neo99 is offline Offline
Newbie Poster

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #5
Nov 21st, 2004
Numbers of days between 1/1/1800 and 21/11/2004 is 74836...check it please..
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 5
Reputation: Neo99 is an unknown quantity at this point 
Solved Threads: 0
Neo99 Neo99 is offline Offline
Newbie Poster

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #6
Nov 21st, 2004
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Logic to Convert Days From 1800 to a Date (Month, Day, Year)

 
0
  #7
Nov 21st, 2004
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC