用C#计算年龄 [英] Calculate Age in C#

查看:134
本文介绍了用C#计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我写一些代码,根据出生日期(以年为单位)来计算特定日期的年龄.

Can someone please help me write some code to calculate age at a particular date based on date of birth in years.

我正在数据库中捕获出生日期和诊断日期,并想通过诊断日期来计算年龄(以年为单位).

I'm capturing date of Birth and Date of Diagnosis in database and would like to calculate age in years by Diagnosis date.

partial void AgeAtDiagnosis_Compute(ref string result)
    {
        // Set result to the desired field value

    }

推荐答案

DateTime birth = new DateTime(1950, 01, 01);
DateTime diagnosis = new DateTime(2012, 02, 01);
TimeSpan Span = diagnosis - birth;
DateTime Age = DateTime.MinValue + Span;
// note: MinValue is 1/1/1 so we have to subtract...
int Years = Age.Year - 1;

这篇关于用C#计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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