C#關於精確年齡的算法(精確到天)


應用場景:

1、已知一個日期點,求該日期到當前日期(今天)的精確年齡;

2、已知兩個日期點,求這兩個日期點的精確年齡;

Code下載請點擊:CalculateAgeCls.cs


 1 namespace ExDevilLee.DateTime
 2 {
 3     /// <summary>
 4     /// 年齡計算工具類
 5     /// 由兩個日期參數計算出精確年齡字符串
 6     /// </summary>
 7     public static class CalculateAgeCls
 8     {
 9         /// <summary>
10         /// 獲得年齡字符串:某個日期點到今天的年齡
11         /// 默認返回:xx歲xx月xx天
12         /// </summary>
13         /// <param name="p_FirstDateTime">第1個日期參數</param>
14         public static string GetAgeString(System.DateTime p_FirstDateTime)
15         {
16             return CalculateAgeString(p_FirstDateTime, System.DateTime.Now, null);
17         }
18         /// <summary>
19         /// 獲得年齡字符串:某個日期點到今天的年齡
20         /// 默認返回:xx歲xx月xx天
21         /// </summary>
22         /// <param name="p_FirstDateTime">第1個日期參數</param>
23         /// <param name="p_Format">返回字符串的格式,默認為:{0}歲{1}月{2}天</param>
24         public static string GetAgeString(System.DateTime p_FirstDateTime, string p_ReturnFormat)
25         {
26             return CalculateAgeString(p_FirstDateTime, System.DateTime.Now, p_ReturnFormat);
27         }
28         /// <summary>
29         /// 獲得年齡字符串:兩個日期點之間的年齡
30         /// 默認返回:xx歲xx月xx天
31         /// </summary>
32         /// <param name="p_FirstDateTime">第1個日期參數</param>
33         /// <param name="p_SecondDateTime">第2個日期參數</param>
34         public static string GetAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime)
35         {
36             return CalculateAgeString(p_FirstDateTime, p_SecondDateTime, null);
37         }
38         /// <summary>
39         /// 獲得年齡字符串:兩個日期點之間的年齡
40         /// 默認返回:xx歲xx月xx天
41         /// </summary>
42         /// <param name="p_FirstDateTime">第1個日期參數</param>
43         /// <param name="p_SecondDateTime">第2個日期參數</param>
44         /// <param name="p_Format">返回字符串的格式,默認為:{0}歲{1}月{2}天</param>
45         public static string GetAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)
46         {
47             return CalculateAgeString(p_FirstDateTime, p_SecondDateTime, p_ReturnFormat);
48         }
49 
50         /// <summary>
51         /// 計算年齡字符串
52         /// 默認返回:xx歲xx月xx天
53         /// </summary>
54         /// <param name="p_FirstDateTime">第1個日期參數</param>
55         /// <param name="p_SecondDateTime">第2個日期參數</param>
56         /// <param name="p_Format">返回字符串的格式,默認為:{0}歲{1}月{2}天</param>
57         private static string CalculateAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)
58         {
59             //判斷時間段是否為正。若為負,調換兩個時間點的位置。
60             if (System.DateTime.Compare(p_FirstDateTime, p_SecondDateTime) > 0)
61             {
62                 System.DateTime stmpDateTime = p_FirstDateTime;
63                 p_FirstDateTime = p_SecondDateTime;
64                 p_SecondDateTime = stmpDateTime;
65             }
66 
67             //判斷返回字符串的格式。若為空,則給默認值:{0}歲{1}月{2}天
68             if (string.IsNullOrEmpty(p_ReturnFormat)) p_ReturnFormat = "{0}歲{1}月{2}天";
69 
70             //定義:年、月、日
71             int year, month, day;
72 
73             //計算:天
74             day = p_SecondDateTime.Day - p_FirstDateTime.Day;
75             if (day < 0)
76             {
77                 day += System.DateTime.DaysInMonth(p_FirstDateTime.Year, p_FirstDateTime.Month);
78                 p_FirstDateTime = p_FirstDateTime.AddMonths(1);
79             }
80             //計算:月
81             month = p_SecondDateTime.Month - p_FirstDateTime.Month;
82             if (month < 0)
83             {
84                 month += 12;
85                 p_FirstDateTime = p_FirstDateTime.AddYears(1);
86             }
87             //計算:年
88             year = p_SecondDateTime.Year - p_FirstDateTime.Year;
89 
90             //返回格式化后的結果
91             return string.Format(p_ReturnFormat, year, month, day);
92         }
93     }
94 }

2014年7月18日 15:24:35

1、對該博文進行了整體的調整,修改了原方法中的BUG;

2、所有函數均調整為靜態函數,便於直接調用;

3、增加參數:返回字符串的格式化(默認值為{0}歲{1}月{2}天),便於修改返回結果格式;

4、對調用函數增加了幾個重載,便於適應多種情況的調用;

備注:在這里非常感謝 冰麟輕武 給予的支持與幫助!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM