hi i wannt ot make a programe in C inwhich user input current date and date of birth, output shows age in the number of years, months, days, etc.,
PLease help me!!!!!!!!!!!!!!

I'm sure I've said something like this before today, but I suggest you start by reading the date of birth into three integer variables, perhaps using scanf.

Let us know how you get on.

Edited 6 Years Ago by Martin B: n/a

time.h has a function named difftime() that returns the difference between two time_t variables in seconds.

Step 1: get birth date.convert it to struct tm the call mktime() which will return the time_t variable.

Step 2: call time() to get current data/time in time_t

Step 3. Call difftime() to get the difference in seconds

Step 4. Convert the result of Step 3 into years, months, days, seconds. To do that is just simply a matter of subtraction.

please say how can we convert seconds to years days etc..
do you mean a day is equivalent to 86400 seconds?? and so on.......

simple --

const int seconds_per_minute = 60;
const int seconds_per_hour = seconds_per_minute * 60;
const int seconds_per_day = seconds_per_hour * 24;
const int seconds_per_year = seconds_per_day * 365;


int seconds = 123444445;
int years = seconds % seconds_per_year;

Do we really want to use functions in time.h?

Three integers for the current date, three integers for the DOB, then find the difference given an array of the lengths of months and the algorithm for checking if a year is a leap year (year divides by 4 and not 400).

Edited 6 Years Ago by Martin B: n/a

so can i get clarity please..
when we have to perform the modulo division operation as you did till the number of seconds is less than the no.., of seconds per year.
am i right??

There is no simple conversion between seconds and months or years.
You need to consider the number of days in each month of a timespan.

Call time() to get the current data/time. This will give you the current time in seconds snce 1970. There is no function in time.h that returns the number of seconds since year 0 because it would be too big a number.

Now, once you have the year, month and day of birth, populate a struct tm with it and call mktime() to covert to seconds sine 1970.

Example:

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

int main()
{
    struct tm tm;
    int month = 1;
    int year = 2010;
    int day = 1;
    time_t today, dob;
    memset(&tm,0,sizeof(struct tm));
    tm.tm_year = year - 1900; // Number of years since 1900
    tm.tm_mon = month-1; // months are 0 based, so that 0 = Jan, 1 = Feb, etc
    tm.tm_mday = day;
    dob = mktime(&tm);

    today = time(0);
    double diff = difftime(today,dob);


    printf("dob = %u\n", dob);
    printf("diff = %lf\n", diff);
}

Edited 6 Years Ago by Ancient Dragon: n/a

sorry for again concentrating on the same question

i got ur code but i think you just printed the seconds instead of age..
but i want to get in terms of years ,months and days
please please how to do this.....

The code I posted earlier will get you the difference in seconds. All you have to do is make the calculations that I posted here.

I'm not going to do all the homewoprk for you. You have to learn how to use your head for something other than a hat rack. This problem is nothing more than 4th grade math. How many years are there in X number of seconds. Any 10 year old should be able to solve that problem.

Edited 6 Years Ago by Ancient Dragon: n/a

@Ancient Dragon, it just isn't as simple as you are implying.

When people talk about an age in years, months and days, they are talking about elapsed calendar years, elapsed calendar months and days.

Consider this:

There are 40 days between May 25 and July 4.
This is a period of 1 month and 10 days (the month of June, 6 days at the end of May, and 4 days at the beginning of July).

There are also 40 days between June 25 and August 4.
This is a period of 1 month and 9 days, however, (the month of July, 5 days at the end of May and 4 days at the beginning of August).

So a timespan cannot be directly converted into years, months and days without reference to the calendar.

This is what I meant when I said that it might not be worth using time.h. You still need to measure the difference against the calendar.

There may be formulas for this, but a simple way of doing it is to generate and cycle throught the calendars between the 2 dates.

Edited 6 Years Ago by Martin B: n/a

This article has been dead for over six months. Start a new discussion instead.