C#精确计算年龄的方法分析
2018-10-12 14:10
633 查看
本文实例讲述了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#计算两个时间的时间差,精确到年月日时分秒
- php根据生日计算年龄的方法
- 年龄Age的计算(可以精确到1岁3个月10天)
- 对tableView三种计算动态行高方法的分析
- 编写一个计算长方体体积的C#WindowsForm,包含一个类,一个方法,三个属性
- Java笔记4:解析Java中的精确计算方法1
- 例题:计算运费。c#语言基础,比较简单。看代码输入格式和方法。同样方法可以做一个 出租车打车的程序
- C#计算代码执行时间的方法
- .NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析
- 【转载】C#时间差的计算,精确输出“年月天时分秒”
- C#常用加密方法分析
- 一个简单方法完成C#时间间隔的计算
- C#实现多线程的同步方法实例分析
- JS浮点数精确计算方法
- C# 计算时间间隔的方法