, . , , Age .
You do not need a year, month, day, or time stamp to do this. Subtract two time periods to get the value in seconds. Divide by 60 * 60 * 24 * 365 until you get the value in years :
int main() {
time_t past = 1234567890;
time_t now = time(0);
double years = double(now - past) / 60 / 60 / 24 / 365;
cout << "years = " << years << '\n';
return 0;
}
This will be disabled by any second seconds that occur between two timestamps, but, to a large extent, it gives the correct value for a year. For example, from December 1, 2010 to January 1, 2011, this is approximately 1/12 year, and not 1 year.
source
share