手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#精确计算年龄的方法分析
C#精确计算年龄的方法分析
摘要:本文实例讲述了C#精确计算年龄的方法。分享给大家供大家参考。具体如下:该源码在vs2010测试通过复制代码代码如下:usingSystem;...

本文实例讲述了C#精确计算年龄的方法。分享给大家供大家参考。具体如下:

该源码在vs2010测试通过

复制代码 代码如下:using System;

using System.Collections.Generic;

using System.Text;

namespace PublicClass

{

public static class CalculationDate

{

/// <summary>

/// 由两个日期计算出年龄(岁、月、天)

/// </summary>

public static void calculationDate(DateTime beginDateTime, DateTime endDateTime)

{

if (beginDateTime > endDateTime)

throw new Exception("开始时间应小于或等与结束时间!");

/*计算出生日期到当前日期总月数*/

int Months = endDateTime.Month - beginDateTime.Month + 12 * (endDateTime.Year - beginDateTime.Year);

/*出生日期加总月数后,如果大于当前日期则减一个月*/

int totalMonth = (beginDateTime.AddMonths(Months) > endDateTime) ? Months - 1 : Months;

/*计算整年*/

int fullYear = totalMonth / 12;

/*计算整月*/

int fullMonth = totalMonth % 12;

/*计算天数*/

DateTime changeDate = beginDateTime.AddMonths(totalMonth);

double days = (endDateTime - changeDate).TotalDays;

}

}

}

希望本文所述对大家的C#程序设计有所帮助。

【C#精确计算年龄的方法分析】相关文章:

c#简单读取文本的实例方法

C#中控制远程计算机的服务的方法

C# 语音功能的实现方法

C#使用非托管代码直接修改字符串的方法

C#访问PostGreSQL数据库的方法

C# 禁用鼠标中间键的方法

C#实现窗体淡入淡出效果的方法总结

C#操作目录与文件的方法步骤

C# 获取属性名的方法

C# 读取指定路径配置文件的方法

精品推荐
分类导航