calculate age from date

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:
[code]
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
", m, d, y); */


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



}





[/code]
«1

Comments

  • Try the changes in [red]red[/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:
    : [code]
    : void calage()
    : {
    : char buff[BUFSIZ];
    : char date[12];
    : int m,d,y;
    : char dateTen;
    : char dateUnit;
    : char dateTotal;
    :
    :
    :
    :
    :
    : struct tm[red]*[/red] t;
    : time_t now; /* time struct*/
    :
    :
    :
    : time(&now); /* put time into the &now variable*/
    : [red]t = localtime(&now); /* exstract the system time*/
    : strftime(buff, sizeof(buff), "%d/%m/%y", t); /* format the string*/
    : [/red]
    :
    :
    : /*printf("m = %d, d = %d, y = %d
    ", m, d, y); */
    :
    :
    : printf("The current date is %s
    ",buff); /* print the date to the screen*/
    :
    :
    :
    : }
    :
    :
    :
    :
    :
    : [/code]
    :

  • 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.
  • : 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.
    :
    [purple]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 [blue]24*365 + number of leap years[/blue] 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...[/purple]
  • [b][red]This message was edited by stober at 2005-8-22 8:39:52[/red][/b][hr]
    [red]WARNING. This will not work with dates prior to 1970[/red]
    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)








  • 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

  • : ** 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) :)
  • In my opinion, you'd better think about whether the year is leap year or not.
  • 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
    #include

    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
    ";
    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;ibirth_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
    ";

    }
    return;
    }



  • : 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
    : #include
    :
    : 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
    ";
    : 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;ibirth_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
    ";
    :
    : }
    : return;
    : }
    :
    :
    :
    :

  • [b][red]This message was edited by Murschech at 2005-8-24 8:55:54[/red][/b][hr]

    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
    #include
    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
    ";
    : : 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;ibirth_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
    ";
    : :
    : : }
    : : return;
    : : }
    : :
    : :
    : :
    : :
    :
    :



  • [b][red]This message was edited by stober at 2005-8-24 10:6:26[/red][/b][hr]
    :
    : 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
    : #include
    : 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.
    [code]
    // 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
    [/code]








  • : [b][red]This message was edited by stober at 2005-8-24 10:6:26[/red][/b][hr]
    : :
    : : 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
    : : #include
    : : 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.
    : [code]
    : // 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
    : [/code]
    :
    :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.
    :
    :
    :
    :
    :
  • : :
    : :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?
    [blue]Yes, you are right, that explains most of the missing days.[/blue]

    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.
    : :
    [blue]My numbers from the previous calculations were so far off I didn't think it would be worth while trying to figure this out.[/blue]


  • : 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.
  • Gee guys, wouldn't this seem simpler? Maybe not...

    [code]
    #include
    #include

    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("
    Today you are %d days old.

    ", (int)etime/86400+byear);

    system("pause");
    return 0;
    }
    [/code]

    Take Care,
    Ed


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories