Home >Backend Development >C++ >How Do I Calculate a Person's Age from Their DateTime Birthday?
This article introduces how to calculate a person's age based on the birthday of DateTime.
Solution:
Calculate a person's age (in the year) based on the birthday of the DateTime type, please follow the steps below:
Store today's date in a variable called
.today
. age = today.Year - birthdate.Year
<code class="language-csharp">// 保存今天的日期。 var today = DateTime.Today; // 计算年龄。 var age = today.Year - birthdate.Year; // 如果是闰年,则返回到此人出生的年份 if (birthdate.Date > today.AddYears(-age)) age--;</code>
This solution assumes that the concept of age is generally consistent with Western culture. If you need different cultural age definitions, such as East Asia's calculation methods, other factors may be considered.
The above is the detailed content of How Do I Calculate a Person's Age from Their DateTime Birthday?. For more information, please follow other related articles on the PHP Chinese website!