在C#中如何计算某人的年龄? [英] How do I calculate someone's age in C#?
本文介绍了在C#中如何计算某人的年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定代表一个人的生日的 DateTime
,如何计算年龄?
Given a DateTime
representing a person's birthday, how do I calculate their age in years?
推荐答案
一个易于理解和简单的解决方案。
An easy to understand and simple solution.
// Save today's date.
var today = DateTime.UtcNow;
// Calculate the age.
var age = today.Year - birthdate.Year;
// Do stuff with it.
if (birthdate > today.AddYears(-age)) age--;
但是,这假设您正在寻找西方的年龄和不使用东亚推算
However, this assumes you are looking for the western idea of age and not using East Asian reckoning.
这篇关于在C#中如何计算某人的年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文