Csharp/C#教程: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#程序设计有所帮助。

您可能感兴趣的文章:C#实现TCP连接信息统计的方法C#统计字符串中数字个数的方法C#统计字符串里中文汉字个数的方法c#英文单词分类统计示例分享C#计算矩阵的秩实例分析C#计算文件MD5校验的方法C#实现统计字数功能的方法

标签: 方法

C语言职工管理系统设计

socket多人聊天程序C语言版(二)

上述就是C#学习教程:C#精确计算年龄的方法分析分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906059.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐