国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#精確計算年齡的方法分析

2020-01-24 02:07:32
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晴隆县| 平遥县| 镇安县| 卓尼县| 鄄城县| 偏关县| 中山市| 如东县| 旌德县| 延安市| 太保市| 广灵县| 祁门县| 上蔡县| 苍梧县| 衡东县| 临泉县| 河源市| 桑植县| 凤山县| 六安市| 盐津县| 海晏县| 大安市| 遵义市| 宜兰县| 榆社县| 丽水市| 陇西县| 连山| 民丰县| 宁德市| 田东县| 张家港市| 永康市| 河间市| 上杭县| 明溪县| 波密县| 宣武区| 苗栗市|