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

來源:互聯網
上載者:User

標籤: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#精確計算年齡的方法分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.