📜  如何根据 DateTime 类型的生日计算某人的年龄? - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:03.530000             🧑  作者: Mango

代码示例1
// Save today's date.
var today = DateTime.Today;

// Calculate the age.
var age = today.Year - birthdate.Year;

// Go back to the year in which the person was born in case of a leap year
if (birthdate.Date > today.AddYears(-age)) age--;