C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
calculate age from date Posted by bazzano on 22 Aug 2005 at 6:00 AM
Hi guys i have the following code and i am trying to ask for user input and then calculate the users age from the system date. So if the user enters 01/01/1985 and it compares it to the current date it should say that they are 7374 days old. How could i calculate that i am having difficuly i have exstracted the system date already.

c ansi program:
void calage()
{
 char buff[BUFSIZ];
 char date[12];
 int m,d,y;
 char dateTen;
 char dateUnit;
 char dateTotal;





 struct tm t;
 time_t now;    /* time struct*/



 time(&now);   /* put time into the &now variable*/
 t = *localtime(&now);     /* exstract the system time*/
 strftime(buff, sizeof(buff), "%d//%m//%y", &t); /* format the string*/



 /*printf("m = %d, d = %d, y = %d\n", m, d, y); */


 printf("The current date is %s\n",buff);    /* print the date to the screen*/



}






Report
Re: calculate age from date Posted by Lundin on 22 Aug 2005 at 6:51 AM
Try the changes in red below and see if it helps:




: Hi guys i have the following code and i am trying to ask for user input and then calculate the users age from the system date. So if the user enters 01/01/1985 and it compares it to the current date it should say that they are 7374 days old. How could i calculate that i am having difficuly i have exstracted the system date already.
:
: c ansi program:
:
: void calage()
: {
:  char buff[BUFSIZ];
:  char date[12];
:  int m,d,y;
:  char dateTen;
:  char dateUnit;
:  char dateTotal;
: 
: 
: 
: 
: 
:  struct tm* t;
:  time_t now;    /* time struct*/
: 
: 
: 
:  time(&now);   /* put time into the &now variable*/
:  t = localtime(&now);     /* exstract the system time*/
:  strftime(buff, sizeof(buff), "%d/%m/%y", t); /* format the string*/
:  
: 
: 
:  /*printf("m = %d, d = %d, y = %d\n", m, d, y); */
: 
: 
:  printf("The current date is %s\n",buff);    /* print the date to the screen*/
: 
: 
: 
: }
: 
: 
: 
: 
: 
: 

:

Report
Re: calculate age from date Posted by bazzano on 22 Aug 2005 at 6:59 AM
but once i have exstracted the system date how could i code the program to caculate the users age from the system date. How would i make it so it works out how many days old they are in dd/mm/yyyy format.
Report
Re: calculate age from date Posted by Donotalo on 22 Aug 2005 at 8:15 AM
: but once i have exstracted the system date how could i code the program to caculate the users age from the system date. How would i make it so it works out how many days old they are in dd/mm/yyyy format.
:
first calculate the number of complete years elapsed. for example, if the user input is 04/12/1980 and the system time is 22/08/2005, then there are complete 24 years. so from here, u will get number of days - 24*365 + number of leap years in between.
next, start calculating days, since different months have different days. for the example above, u r calculated 24*365 + number of leap years days, so imagine u r at 04/12/2004 date. when advance one month, i.e. reached at 04/01/2005, add 31 days, since december has 31 days. now advance another 31 days and one month, and calculate so on...

Report
Re: calculate age from date Posted by stober on 22 Aug 2005 at 8:33 AM
This message was edited by stober at 2005-8-22 8:39:52

WARNING. This will not work with dates prior to 1970
1. leave the current date in integer format returned by time() function. Don't convert it to struct tm or other format.
2. convert the birthdate to integer format. To do that, set struct tm object to birthdate fields and call mktime() to normalize it and convert it to integer. you need to memset the entire structure to 0s before you begin, otherwise mktime() will not work correctly. you have to set the day (1-31), month (0-11), year (current - 1900), hour and minute fields of the structure. If hour and minute are not important (which they probably aren't in your project), just use some fake value, such as 12:00 noon.
3. subtract the two integers. This will give you the difference in seconds, which now can be easily converted again to whatever format you like. For example, if you want the number of days, then take seconds and divide by 3600 (the number of seconds in one day)








Report
Re: calculate age from date Posted by Ed Hall on 22 Aug 2005 at 8:49 PM
I haven't tried to code this yet, but would the following approach be feasible to take care of the 1970 issue?

1. - Enter date.
2. - Make temporary date using entered day and month combined with 1970.
3. - Compute entered year difference with 1970 times 365 + year difference divided by 4 (leap years).
4. - mktime(temporary date)
5. - time(&now) for today
6. - Calculate integer difference and divide by 86400** to get days.
7. - Sum days achieved in 3 above with days achieved in 6 above.

Would this take care of the pre-1970 issue?
Would post 1970 entered dates create a negative number in step 2 which would subtract itself when summed with the number of days from 1970 to today?

** I think there are 86400 seconds in a day rather than 3600. I think that's in an hour. 60*60*24 for a day?

Take Care,
Ed

Report
Re: calculate age from date Posted by stober on 23 Aug 2005 at 4:33 AM
: ** I think there are 86400 seconds in a day rather than 3600. I think that's in an hour. 60*60*24 for a day?
:

Ed -- I don't know about you but my days only have 3600 seconds (1 hour) :)
Report
Re: calculate age from date Posted by zyb365 on 23 Aug 2005 at 10:29 PM
In my opinion, you'd better think about whether the year is leap year or not.
Report
Re: calculate age from date Posted by zyb365 on 24 Aug 2005 at 4:55 AM
Oh, it's not very difficult.Here is a simple sample for your requirement. You can change the code easily in order to realize what you need. I hope it can help you.

#include<iostream.h>
#include<stdlib.h>

int current_day=24; //current time
int current_month=8;
int current_year= 2005;

class Caculate{
public:
int birth_day;
int birth_month;
int birth_year;

public:
Caculate();
int getMonthDays(int month);
bool judgeYear(int year);
void inputBirthday();
int returndays();
~Caculate();
};
Caculate::Caculate()
{
}
int Caculate::getMonthDays(int month)
{
int days;
switch(month){
case 2:
days=28;
if(Caculate::judgeYear(current_year))
days=29;
break;
case 4:
days=30;
break;
case 6:
days=30;
break;
case 9:
days=30;
break;
case 11:
days=30;
break;
default:
days=31;
break;
}
return days;
}
bool Caculate::judgeYear(int m)
{
int n;
n=m%100;
if((n==0 && m%400==0)||(n!=0 && m/4==0))
return true; //This year is leap year
else
return false;
}
void Caculate::inputBirthday()
{
cout<<"Please input your birthday dd/mm/yy:";
cin>>birth_day>>birth_month>>birth_year;
while((birth_day>31 || birth_day<1)||(birth_month>12 || birth_month<1)||(birth_year>2005 || birth_year< 1880))
{
cout<<"your birthday is illegal ,input again \n";
cin>>birth_day>>birth_month>>birth_year;
}

return;
}
int Caculate::returndays()
{
int days,i ;
if(current_day >= birth_day)
days=current_day - birth_day;
else
{
if (current_month !=1)
days=Caculate::getMonthDays(--current_month)+current_day-birth_day;
else
{
days=31+current_day-birth_day;
current_year--;
current_month=12;
}
}
if(current_month > birth_month)
{
for ( i=birth_month;i<current_month;i++)
days=days+getMonthDays(i);
};
if(current_month<birth_month)

{
for ( i=birth_month;i<=12;i++)
days=days+getMonthDays(i);

for( i=1;i<current_month;i++)
days=days+getMonthDays(i);

current_year--;

};
if (current_year>birth_year)
{
int m=0;
for(i=birth_year; i<current_year;i++)
{
if(Caculate::judgeYear(i))
m++;
}
days=365*(current_year - birth_year)+m;
}
return days; //return the end

}
Caculate::~Caculate()
{

}
void main()
{
while(1){
Caculate co;
co.inputBirthday();
cout<<"Your age is:"<<co.returndays()<< " days\n";

}
return;
}



Report
Re: calculate age from date Posted by Murschech on 24 Aug 2005 at 8:40 AM
: Oh, it's not very difficult.Here is a simple sample for your requirement. You can change the code easily in order to realize what you need. I hope it can help you.
:
: #include<iostream.h>
: #include<stdlib.h>
:
: int current_day=24; //current time
: int current_month=8;
: int current_year= 2005;
:
: class Caculate{
: public:
: int birth_day;
: int birth_month;
: int birth_year;
:
: public:
: Caculate();
: int getMonthDays(int month);
: bool judgeYear(int year);
: void inputBirthday();
: int returndays();
: ~Caculate();
: };
: Caculate::Caculate()
: {
: }
: int Caculate::getMonthDays(int month)
: {
: int days;
: switch(month){
: case 2:
: days=28;
: if(Caculate::judgeYear(current_year))
: days=29;
: break;
: case 4:
: days=30;
: break;
: case 6:
: days=30;
: break;
: case 9:
: days=30;
: break;
: case 11:
: days=30;
: break;
: default:
: days=31;
: break;
: }
: return days;
: }
: bool Caculate::judgeYear(int m)
: {
: int n;
: n=m%100;
: if((n==0 && m%400==0)||(n!=0 && m/4==0))
: return true; //This year is leap year
: else
: return false;
: }
: void Caculate::inputBirthday()
: {
: cout<<"Please input your birthday dd/mm/yy:";
: cin>>birth_day>>birth_month>>birth_year;
: while((birth_day>31 || birth_day<1)||(birth_month>12 || birth_month<1)||(birth_year>2005 || birth_year< 1880))
: {
: cout<<"your birthday is illegal ,input again \n";
: cin>>birth_day>>birth_month>>birth_year;
: }
:
: return;
: }
: int Caculate::returndays()
: {
: int days,i ;
: if(current_day >= birth_day)
: days=current_day - birth_day;
: else
: {
: if (current_month !=1)
: days=Caculate::getMonthDays(--current_month)+current_day-birth_day;
: else
: {
: days=31+current_day-birth_day;
: current_year--;
: current_month=12;
: }
: }
: if(current_month > birth_month)
: {
: for ( i=birth_month;i<current_month;i++)
: days=days+getMonthDays(i);
: };
: if(current_month<birth_month)
:
: {
: for ( i=birth_month;i<=12;i++)
: days=days+getMonthDays(i);
:
: for( i=1;i<current_month;i++)
: days=days+getMonthDays(i);
:
: current_year--;
:
: };
: if (current_year>birth_year)
: {
: int m=0;
: for(i=birth_year; i<current_year;i++)
: {
: if(Caculate::judgeYear(i))
: m++;
: }
: days=365*(current_year - birth_year)+m;
: }
: return days; //return the end
:
: }
: Caculate::~Caculate()
: {
:
: }
: void main()
: {
: while(1){
: Caculate co;
: co.inputBirthday();
: cout<<"Your age is:"<<co.returndays()<< " days\n";
:
: }
: return;
: }
:
:
:
:

Report
Re: calculate age from date Posted by Murschech on 24 Aug 2005 at 8:42 AM
This message was edited by Murschech at 2005-8-24 8:55:54


The kind of computation you're looking for is very familiar to anyone who's played with astronomical computatios. The astronomer's "Julian Day Number" is the number of days that have passed from some fixed reference date. (I forget what that date is. It doesn't matter). The ideas needed to create such a program have been mentioned by previous contributors to this thread. Here is a program to compute the number of days which have passed since Jan. 1, 401. It's a lot shorter than the one written by zyb365, but it's based on the same idea. ("MJDN" stands for "modified Julian day number".)


struct date { int month; int day ; int year; };
#include <iostream.h>
#include <stdio.h>
long MJDN(date dt);

main()
{ date dt;

cout << "Enter day of the month: ";
cin >> dt.day;
cout << "Enter month number: ";
cin >> dt.month;
cout << "Enter year: ";
cin >> dt.year;
cout << MJDN(dt) <<endl;
getchar();
}

long MJDN(date dt)
// Returns number of days passed since Jan. 1, 401
{ int k, DOY, ly;

ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
if (!ly) ly += 2;
//ly is 1 for leap year, else 2
k = dt.year - 401;
DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
// DOY is "day of year".
return (365*k + k/4 -k/100 +k/400 +DOY -1);
}














: :
: : public:
: : Caculate();
: : int getMonthDays(int month);
: : bool judgeYear(int year);
: : void inputBirthday();
: : int returndays();
: : ~Caculate();
: : };
: : Caculate::Caculate()
: : {
: : }
: : int Caculate::getMonthDays(int month)
: : {
: : int days;
: : switch(month){
: : case 2:
: : days=28;
: : if(Caculate::judgeYear(current_year))
: : days=29;
: : break;
: : case 4:
: : days=30;
: : break;
: : case 6:
: : days=30;
: : break;
: : case 9:
: : days=30;
: : break;
: : case 11:
: : days=30;
: : break;
: : default:
: : days=31;
: : break;
: : }
: : return days;
: : }
: : bool Caculate::judgeYear(int m)
: : {
: : int n;
: : n=m%100;
: : if((n==0 && m%400==0)||(n!=0 && m/4==0))
: : return true; //This year is leap year
: : else
: : return false;
: : }
: : void Caculate::inputBirthday()
: : {
: : cout<<"Please input your birthday dd/mm/yy:";
: : cin>>birth_day>>birth_month>>birth_year;
: : while((birth_day>31 || birth_day<1)||(birth_month>12 || birth_month<1)||(birth_year>2005 || birth_year< 1880))
: : {
: : cout<<"your birthday is illegal ,input again \n";
: : cin>>birth_day>>birth_month>>birth_year;
: : }
: :
: : return;
: : }
: : int Caculate::returndays()
: : {
: : int days,i ;
: : if(current_day >= birth_day)
: : days=current_day - birth_day;
: : else
: : {
: : if (current_month !=1)
: : days=Caculate::getMonthDays(--current_month)+current_day-birth_day;
: : else
: : {
: : days=31+current_day-birth_day;
: : current_year--;
: : current_month=12;
: : }
: : }
: : if(current_month > birth_month)
: : {
: : for ( i=birth_month;i<current_month;i++)
: : days=days+getMonthDays(i);
: : };
: : if(current_month<birth_month)
: :
: : {
: : for ( i=birth_month;i<=12;i++)
: : days=days+getMonthDays(i);
: :
: : for( i=1;i<current_month;i++)
: : days=days+getMonthDays(i);
: :
: : current_year--;
: :
: : };
: : if (current_year>birth_year)
: : {
: : int m=0;
: : for(i=birth_year; i<current_year;i++)
: : {
: : if(Caculate::judgeYear(i))
: : m++;
: : }
: : days=365*(current_year - birth_year)+m;
: : }
: : return days; //return the end
: :
: : }
: : Caculate::~Caculate()
: : {
: :
: : }
: : void main()
: : {
: : while(1){
: : Caculate co;
: : co.inputBirthday();
: : cout<<"Your age is:"<<co.returndays()<< " days\n";
: :
: : }
: : return;
: : }
: :
: :
: :
: :
:
:



Report
Re: calculate age from date Posted by stober on 24 Aug 2005 at 9:55 AM
This message was edited by stober at 2005-8-24 10:6:26

:
: The kind of computation you're looking for is very familiar to anyone who's played with astronomical computatios. The astronomer's "Julian Day Number" is the number of days that have passed from some fixed reference date. (I forget what that date is. It doesn't matter). The ideas needed to create such a program have been mentioned by previous contributors to this thread. Here is a program to compute the number of days which have passed since Jan. 1, 401. It's a lot shorter than the one written by zyb365, but it's based on the same idea. ("MJDN" stands for "modified Julian day number".)
:
:
: struct date { int month; int day ; int year; };
: #include <iostream.h>
: #include <stdio.h>
: long MJDN(date dt);
:
: main()
: { date dt;
:
: cout << "Enter day of the month: ";
: cin >> dt.day;
: cout << "Enter month number: ";
: cin >> dt.month;
: cout << "Enter year: ";
: cin >> dt.year;
: cout << MJDN(dt) <<endl;
: getchar();
: }
:
: long MJDN(date dt)
: // Returns number of days passed since Jan. 1, 401
: { int k, DOY, ly;
:
: ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: if (!ly) ly += 2;
: //ly is 1 for leap year, else 2
: k = dt.year - 401;
: DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: // DOY is "day of year".
: return (365*k + k/4 -k/100 +k/400 +DOY -1);
: }
:

Interesting algorithm, but I question its accuracy. According to your algorithm there are 586084 days between today (24 Aug 2005) and 1 Jan 401. But if I calculate it manually I can only come up with 585,730 days, or a difference of 354 days which is almost a full year.
// number of full years since 1 Jan 401
2004 - 401 = 1603
// convert years to days
1603 * 365 = 585,095
// account for leap years
1603 / 4 = 400 leap years since 401.
585,095 + 400 = 585,495
// number of days since 1 Jan 2005
585,495 + 235 = 575,730









Report
Re: calculate age from date Posted by Murschech on 24 Aug 2005 at 9:33 PM
: This message was edited by stober at 2005-8-24 10:6:26

: :
: : The kind of computation you're looking for is very familiar to anyone who's played with astronomical computatios. The astronomer's "Julian Day Number" is the number of days that have passed from some fixed reference date. (I forget what that date is. It doesn't matter). The ideas needed to create such a program have been mentioned by previous contributors to this thread. Here is a program to compute the number of days which have passed since Jan. 1, 401. It's a lot shorter than the one written by zyb365, but it's based on the same idea. ("MJDN" stands for "modified Julian day number".)
: :
: :
: : struct date { int month; int day ; int year; };
: : #include <iostream.h>
: : #include <stdio.h>
: : long MJDN(date dt);
: :
: : main()
: : { date dt;
: :
: : cout << "Enter day of the month: ";
: : cin >> dt.day;
: : cout << "Enter month number: ";
: : cin >> dt.month;
: : cout << "Enter year: ";
: : cin >> dt.year;
: : cout << MJDN(dt) <<endl;
: : getchar();
: : }
: :
: : long MJDN(date dt)
: : // Returns number of days passed since Jan. 1, 401
: : { int k, DOY, ly;
: :
: : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : if (!ly) ly += 2;
: : //ly is 1 for leap year, else 2
: : k = dt.year - 401;
: : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : // DOY is "day of year".
: : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : }
: :
:
: Interesting algorithm, but I question its accuracy. According to your algorithm there are 586084 days between today (24 Aug 2005) and 1 Jan 401. But if I calculate it manually I can only come up with 585,730 days, or a difference of 354 days which is almost a full year.
:
: // number of full years since 1 Jan 401
: 2004 - 401 = 1603
: // convert years to days
: 1603 * 365 = 585,095
: // account for leap years
: 1603 / 4 = 400 leap years since 401.
: 585,095 + 400 = 585,495
: // number of days since 1 Jan 2005
: 585,495 + 235 = 575,730
: 

:
:It's no surprise that you get about a full year off. The number of full years from Jan 1 401 to Jan 1 2005 is 2005 -401. Why did you write 2004 - 401? Also your number of leap years is not correct. Perhaps you're not aware of the fact that a year which is a multiple of 100 but not 400 is not a leap year, so that, for example, the years 500, 600 and 700 are not leap years, but 800 is. You seem to be counting them as leap years. The number of leap years in the years 401,402..2004 is 1604/4 -(1604)/100
+1604/400. If you make these corrections you come up with 586084.
:
If you search the web for "Julian Day Number" you'll find many versions of this program which you can run on the web or download.

By the way, an algorithm to convert from Julian day number to Gregorian date (which is our everyday civil calendar) is a bit more complex, but very interesting.
:
:
:
:
:
Report
Re: calculate age from date Posted by stober on 25 Aug 2005 at 4:30 AM
: :
: :It's no surprise that you get about a full year off. The number of full years from Jan 1 401 to Jan 1 2005 is 2005 -401. Why did you write 2004 - 401?
Yes, you are right, that explains most of the missing days.

Also your number of leap years is not correct. Perhaps you're not aware of the fact that a year which is a multiple of 100 but not 400 is not a leap year, so that, for example, the years 500, 600 and 700 are not leap years, but 800 is. You seem to be counting them as leap years. The number of leap years in the years 401,402..2004 is 1604/4 -(1604)/100
: +1604/400. If you make these corrections you come up with 586084.
: :
My numbers from the previous calculations were so far off I didn't think it would be worth while trying to figure this out.


Report
Re: calculate age from date Posted by Ed Hall on 26 Aug 2005 at 6:57 AM
I haven't fully understood your entire code yet, but I don't see the adjustment for the change to the current (Gregorian) calendar which occurred in different places and different years around the world. In 1582, 11 days were lost in part of the world and as recent as 1752 a different part lost 12 days. This wouldn't effect someone's age calculation since few would admit to being that old, but 1 Jan 401 would surely be affected.

That is probably why there is a fixed day for the Julian calculation.

Take Care,
Ed

Report
Re: calculate age from date Posted by Murschech on 26 Aug 2005 at 2:51 PM
: I haven't fully understood your entire code yet, but I don't see the adjustment for the change to the current (Gregorian) calendar which occurred in different places and different years around the world. In 1582, 11 days were lost in part of the world and as recent as 1752 a different part lost 12 days. This wouldn't effect someone's age calculation since few would admit to being that old, but 1 Jan 401 would surely be affected.
:
: That is probably why there is a fixed day for the Julian calculation.
:
: Take Care,
: Ed
:
:This algorithm assumes the date is the Gregorian date. (The word "Julian" in "Julian day number has no connection to the Julian calendar). That is to say, the function returns the number of days passed since 1/1/401 on the Gregorian calendar (though of course, no one was using the Gregorian calendar at that time) and the input date, also assumed to be a date on the Gregorian calendar.

Report
Re: calculate age from date Posted by Lundin on 25 Aug 2005 at 4:56 AM
: long MJDN(date dt)
: // Returns number of days passed since Jan. 1, 401
: { int k, DOY, ly;
:
: ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: if (!ly) ly += 2;
: //ly is 1 for leap year, else 2
: k = dt.year - 401;
: DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: // DOY is "day of year".
: return (365*k + k/4 -k/100 +k/400 +DOY -1);
: }


Without paying much attention to if the algorithm itself is correct or not: Either use double or multiply the ints to something larger before you divide, or you will get a very inaccurate algorithm.
Report
Re: calculate age from date Posted by Ed Hall on 25 Aug 2005 at 8:42 AM
Gee guys, wouldn't this seem simpler? Maybe not...

#include <stdio.h>
#include <time.h>

int main( void )
{
   struct tm temp;
   time_t now, intermediate;
   int    bmonth, bday, byear;
   double etime;
   
   printf("Enter your Birthday in this format (MM DD YYYY):");
   scanf("%d%d%d", &bmonth, &bday, &byear);
   
   byear = ((365.25*(byear-1970))+.5)*-1;

   time( &now );
   temp = *localtime( &now );
   temp.tm_year = 70;
   temp.tm_mon = bmonth-1;
   temp.tm_mday = bday;
   intermediate = mktime( &temp );
   etime = difftime(now, intermediate);
   printf("\nToday you are %d days old.\n\n", (int)etime/86400+byear);
     
   system("pause");
   return 0;
}


Take Care,
Ed


Report
Re: calculate age from date Posted by stober on 25 Aug 2005 at 3:32 PM
yes it would -- but the problem is that standard time functions in MS-Windows can't be used for years before 1 Jan 1970. So if you were born on 31 Dec 1969 those functions will fail.
Report
Re: calculate age from date Posted by Ed Hall on 25 Aug 2005 at 7:59 PM
This message was edited by Ed Hall at 2005-8-26 6:28:31

: yes it would -- but the problem is that standard time functions in MS-Windows can't be used for years before 1 Jan 1970. So if you were born on 31 Dec 1969 those functions will fail.
:
Did you try it, or look at it, or did you just glance and assume?

I think you'd find that it works for a little further than your assumption. I tried it back into the 1800's... (Not too many will admit to being older.)

If you look close, all the questionable time.h functions are limited to occurring after 31 Dec 69. Anything older is mathematically calculated.

I don't think it will work prior to the change to the Gregorian system in the 1500's and it would probably add a leap day erroneously for the 1800, 1700, 1600 years, but those catches could be added if necessary.

oops, 1600 would have had a leap day...

Take Care,
Ed



Report
Re: calculate age from date Posted by stober on 25 Aug 2005 at 8:28 PM
no, I didn't try it -- I saw the time functions and ASSUMED (wrongly) it wouldn't work. My apolologies.

Report
Re: calculate age from date Posted by Ed Hall on 26 Aug 2005 at 6:36 AM
: no, I didn't try it -- I saw the time functions and ASSUMED (wrongly) it wouldn't work. My apolologies.
:
:
No real biggie, except that I had hoped to have covered that specific issue. The issue I would really have to work at would be how to cover the missing days, and in which part of the world for the change to the current (Gregorian) calendar. That would take some study. They skipped a different number of days, at a different time, in different countries. Now there's an ineresting algorithm... Thanks for all, Stober. I appreciate all the information you so feeely provide.

Take Care,
Ed

Report
Re: calculate age from date Posted by Murschech on 25 Aug 2005 at 10:30 PM
: : long MJDN(date dt)
: : // Returns number of days passed since Jan. 1, 401
: : { int k, DOY, ly;
: :
: : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : if (!ly) ly += 2;
: : //ly is 1 for leap year, else 2
: : k = dt.year - 401;
: : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : // DOY is "day of year".
: : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : }
:
:
: Without paying much attention to if the algorithm itself is correct or not: Either use double or multiply the ints to something larger before you divide, or you will get a very inaccurate algorithm.
:

Report
Re: calculate age from date Posted by Murschech on 25 Aug 2005 at 10:37 PM
: : : long MJDN(date dt)
: : : // Returns number of days passed since Jan. 1, 401
: : : { int k, DOY, ly;
: : :
: : : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : : if (!ly) ly += 2;
: : : //ly is 1 for leap year, else 2
: : : k = dt.year - 401;
: : : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : : // DOY is "day of year".
: : : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : : }
: :
: :
: : Without paying much attention to if the algorithm itself is correct or not: Either use double or multiply the ints to something larger before you divide, or you will get a very inaccurate algorithm.
: :
:
:
I guess I've become spoiled. My original version of this program uses long rather than int. That's because I wrote it with ancient turbo c compiler. (You wouldn't believe how old I am.) With a more modern compiler, such as Borland bcc32, int can handle 9 or ten digit numbers (or maybe more.) Since the MJDN of 1/1/3000 is 949,265 it's quite safe to use this program to that date and way beyond. You're right in saying that this program would fail if it were compiled with turbo c.
Report
Re: calculate age from date Posted by Lundin on 25 Aug 2005 at 11:15 PM
: : : : long MJDN(date dt)
: : : : // Returns number of days passed since Jan. 1, 401
: : : : { int k, DOY, ly;
: : : :
: : : : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : : : if (!ly) ly += 2;
: : : : //ly is 1 for leap year, else 2
: : : : k = dt.year - 401;
: : : : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : : : // DOY is "day of year".
: : : : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : : : }
: : :
: : :
: : : Without paying much attention to if the algorithm itself is correct or not: Either use double or multiply the ints to something larger before you divide, or you will get a very inaccurate algorithm.
: : :
: :
: :
: I guess I've become spoiled. My original version of this program uses long rather than int. That's because I wrote it with ancient turbo c compiler. (You wouldn't believe how old I am.) With a more modern compiler, such as Borland bcc32, int can handle 9 or ten digit numbers (or maybe more.) Since the MJDN of 1/1/3000 is 949,265 it's quite safe to use this program to that date and way beyond. You're right in saying that this program would fail if it were compiled with turbo c.
:

That is right, but it wasn't what I tried to say. If you divide with integers the result will be truncated.
Report
Re: calculate age from date Posted by Murschech on 26 Aug 2005 at 2:40 PM
: : : : : long MJDN(date dt)
: : : : : // Returns number of days passed since Jan. 1, 401
: : : : : { int k, DOY, ly;
: : : : :
: : : : : ly = !(dt.year % 400) ||(!(dt.year % 4)&&(dt.year % 100));
: : : : : if (!ly) ly += 2;
: : : : : //ly is 1 for leap year, else 2
: : : : : k = dt.year - 401;
: : : : : DOY = (275*dt.month)/9 -ly*((dt.month+9)/12)+dt.day - 30;
: : : : : // DOY is "day of year".
: : : : : return (365*k + k/4 -k/100 +k/400 +DOY -1);
: : : : : }
: : : :
: : : :
: : : : Without paying much attention to if the algorithm itself is correct or not: Either use double or multiply the ints to something larger before you divide, or you will get a very inaccurate algorithm.
: : : :
: : :
: : :
: : I guess I've become spoiled. My original version of this program uses long rather than int. That's because I wrote it with ancient turbo c compiler. (You wouldn't believe how old I am.) With a more modern compiler, such as Borland bcc32, int can handle 9 or ten digit numbers (or maybe more.) Since the MJDN of 1/1/3000 is 949,265 it's quite safe to use this program to that date and way beyond. You're right in saying that this program would fail if it were compiled with turbo c.
: :
:
: That is right, but it wasn't what I tried to say. If you divide with integers the result will be truncated.
:

True, but that's what makes the algorithm work! For instance, look at the term k/4 in the last line. k is the difference between the current year and 401. k/4 is the number of years which are multiples of 4. For instance, say the year in which you're interested is 1990. Then k = 1990-401 = 1589 and 1589/4 = 497 (in integer arithmetic). Then of the 1589 numbers 401, 402, 403,... 1989, 497 are multiples of 4. (If this isn't clear, try it with the years 402, 403, 404 and 405 rather than 1990).


1 2  Next



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.