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、对调用函数增加了几个重载,便于适应多种情况的调用;

备注:在这里非常感谢 冰麟轻武 给予的支持与帮助!

posted on 2013-12-11 15:16  ExDevilLee  阅读(4768)  评论(12编辑  收藏  举报

导航