A person's age is very simple to calculate the age of a person based on the birth date of the DateTime type.
Solution:
First, get the current date to the Datetime variable named TODAY. Then, calculate the difference between the current year and the yeardate.year as the age of the person. The code is as follows:
Leap year adjustment:
<code>var today = DateTime.Today; var age = today.Year - birthdate.Year;</code>
Note:
This solution calculates age based on the concept of Western age. If the East Asian algorithm is required, further modification may be required.
<code>if (birthdate.Date > today.AddYears(-age)) age--;</code>
The above is the detailed content of How Can I Calculate a Person's Age from Their Birthdate Using DateTime?. For more information, please follow other related articles on the PHP Chinese website!