计算两个日期之间的差异,并以几年取值 [英] Calculate the difference between two dates and get the value in years?

查看:105
本文介绍了计算两个日期之间的差异,并以几年取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我想基本计算员工的年龄 - 所以我们每个员工都有DOB,所以在
C#Side我想做这样的事情 -

I want to calculate basically the age of employees - So we have DOB for each employee, So on the C# Side I want to do something like this -

int age=Convert.Int32(DateTime.Now-DOB);

我可以使用日子,操纵然后获得年龄...但是我想知道是否有东西我可以直接使用这个年数。

I can use days and manipulate then get the age...but I wanted to know if there something I can use directly to get the number of years.

推荐答案

你想计算一个员工的年龄吗?然后,您可以使用此代码段(从如何计算某人的年龄在C# ?):

Do you want calculate the age in years for an employee? Then you can use this snippet (from How do I calculate someone's age in C#?):

DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;

如果没有,请指定。我很难理解你想要什么。

If not, then please specify. I'm having a hard time understanding what you want.

这篇关于计算两个日期之间的差异,并以几年取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆