I am having trouble using the CTime and CTimespan classes. I need to use them to create an object of this class with my date of birth and then using the ctimespan object subtract the two to give my age,
Does anyone have any code or tips that would help me out?
You don't need to use a CTimeSpan object to do this, you can just use two CTime objects and get your age by using the GetYear(), GetMonth() functions etc.
this example is only accurate to the month but you can modify it to be accurate to the day and it should give you an idea of what you need to do:
CTime now(CTime::GetCurrentTime());
CTime birthdate(1972, 5, 28, 0, 0, 0);
int age = now.GetYear() - birthdate.GetYear();
if(now.GetMonth() < birthdate.GetMonth())
age--;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.