Calculate the age according to the date of birth
The DateTime type, known as a personal birthday, is a simple task to determine that their age (in the year) is a simple task.
Solution
To calculate someone’s age based on DateTime's birthday, please follow the steps below:
Sign up today's date to a Datetime variable, for example,
.DateTime today = DateTime.Today;
. int age = today.Year - birthdate.Year;
), the age will be reduced by 1: birthdate.Date > today.AddYears(-age)
if (birthdate.Date > today.AddYears(-age)) age--;
Precautions This calculation assumes that the concept of "Western" is used, and the age increases on the day of birthday. In contrast, in the "East Asian algorithm", the first day of the new year increased. For more complicated age calculations, consider using a library or calendar system that considers regional customs.
The above is the detailed content of How Do I Calculate a Person's Age from Their Birthdate (DateTime)?. For more information, please follow other related articles on the PHP Chinese website!