• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#精确计算年龄的另类方法

OC/C/C++ 水墨上仙 2370次浏览

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#精确计算年龄的另类方法
喜欢 (0)
加载中……