欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#精確計(jì)算年齡的方法分析

 更新時(shí)間:2015年03月20日 15:44:21   作者:kimsung  
這篇文章主要介紹了C#精確計(jì)算年齡的方法,實(shí)例分析了C#計(jì)算時(shí)間的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#精確計(jì)算年齡的方法。分享給大家供大家參考。具體如下:

該源碼在vs2010測(cè)試通過(guò)

復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace PublicClass
{
    public static class CalculationDate
    {
        /// <summary>
        /// 由兩個(gè)日期計(jì)算出年齡(歲、月、天)
        /// </summary>
        public static void calculationDate(DateTime beginDateTime, DateTime endDateTime)
        {
            if (beginDateTime > endDateTime)
                throw new Exception("開(kāi)始時(shí)間應(yīng)小于或等與結(jié)束時(shí)間!");
            /*計(jì)算出生日期到當(dāng)前日期總月數(shù)*/
            int Months = endDateTime.Month - beginDateTime.Month + 12 * (endDateTime.Year - beginDateTime.Year);
            /*出生日期加總月數(shù)后,如果大于當(dāng)前日期則減一個(gè)月*/
            int totalMonth = (beginDateTime.AddMonths(Months) > endDateTime) ? Months - 1 : Months;
            /*計(jì)算整年*/
            int fullYear = totalMonth / 12;
            /*計(jì)算整月*/
            int fullMonth = totalMonth % 12;
            /*計(jì)算天數(shù)*/
            DateTime changeDate = beginDateTime.AddMonths(totalMonth);
            double days = (endDateTime - changeDate).TotalDays;
        }
    }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論