C#精确计算年龄的方法分析
本文实例讲述了C#精确计算年龄的方法。分享给大家供大家参考。具体如下:
该源码在vs2010测试通过
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespacePublicClass
{
publicstaticclassCalculationDate
{
///<summary>
///由两个日期计算出年龄(岁、月、天)
///</summary>
publicstaticvoidcalculationDate(DateTimebeginDateTime,DateTimeendDateTime)
{
if(beginDateTime>endDateTime)
thrownewException("开始时间应小于或等与结束时间!");
/*计算出生日期到当前日期总月数*/
intMonths=endDateTime.Month-beginDateTime.Month+12*(endDateTime.Year-beginDateTime.Year);
/*出生日期加总月数后,如果大于当前日期则减一个月*/
inttotalMonth=(beginDateTime.AddMonths(Months)>endDateTime)?Months-1:Months;
/*计算整年*/
intfullYear=totalMonth/12;
/*计算整月*/
intfullMonth=totalMonth%12;
/*计算天数*/
DateTimechangeDate=beginDateTime.AddMonths(totalMonth);
doubledays=(endDateTime-changeDate).TotalDays;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志