標籤:sqli play 表單 des style splay top span space
本文執行個體講述了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#精確計算年齡的方法分析
本文地址: http://www.paobuke.com/develop/c-develop/pbk23104.html
相關內容C#入門教程之集合ArrayList用法詳解C#操作SQLite資料庫方法小結(建立,串連,插入,查詢,刪除等)C#實現表單間傳值執行個體分析C#畫筆使用複合數組繪製單個矩形的方法
C#中IEnumerable、ICollection、IList、List之間的區別C#計算檔案MD5校正的方法C#的path.GetFullPath 擷取上級目錄實現方法C#泛型Dictionary的用法執行個體詳解
C#精確計算年齡的方法分析