C#精确计算年龄的另类方法
2018-01-12 08:22
309 查看
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; |
} |
} |
} |
//该代码片段来自于: http://www.sharejs.com/codes/csharp/9034 |
相关文章推荐
- C#精确计算年龄的方法分析
- C#实现计算年龄的简单方法汇总
- C#中计算时差精确到微秒的方法
- C#计算字符串中子串出现次数的另类方法
- 年龄Age的计算(可以精确到1岁3个月10天)
- C# 中使用 MD5 算法计算 hash (哈希)值的四种方法
- C#基于NPOI生成具有精确列宽行高的Excel文件的方法
- 根据身份证号计算年龄 15位或18位身份证号的年龄计算方法
- 年月日的计算方式 适用于日期的精确计算和方法 财务通用方式
- C# 根据出生日期(年月日)计算年龄的代码
- C# 计算时间间隔的方法
- 精确时间计算方法
- C#获得方法执行的精确时间
- C# 计算时间间隔的方法
- 根据路由器MAC地址精确计算PIN码第八位的方法
- c#中字符串MD5的计算方法
- 计算时间差(精确到毫秒)(getTime()方法)
- C# 时间相减的计算方法(获取时间间隔)
- C#基于基姆拉尔森算法计算指定日期是星期几的方法
- JS浮点数精确计算方法