Home > Net >  Qt calculate age from a QDate
Qt calculate age from a QDate

Time:07-12

I have a birthdate stored as QDate and I would like to kown how many years the person has.
I've tried the daysTo() function but then I cannot convert days in years.
How can I do?

CodePudding user response:

You might want to do this:

int age(const QDate &birthday)
{
    const auto today = QDate::currentDate();
    auto age = today.year() - birthday.year();
    return today.month() >= birthday.month() && today.day() >= birthday.day() ? age : age - 1;
}
  •  Tags:  
  • qt
  • Related