通过出生日期和当前日期计算年,月,天(我的年龄、在职日期)

常见应用

我多大了,在职时间

C#代码:

 1     /// 通过出生日期和当前日期计算年,月,天
 2     /// </summary>
 3     /// <param name="birth">出生日期</param>
 4     /// <param name="now">当前日期</param>
 5     /// <param name="year"></param>
 6     /// <param name="month"></param>
 7     /// <param name="day"></param>
 8     private void GetDateByBirth(DateTime birth, DateTime now, out int year, out int month,
 9     out int day)
10     {
11         //出生日期的年,月,日
12         int birthYear = birth.Year;
13         int birthMonth = birth.Month;
14         int birthDay = birth.Day;
15 
16         //当前时间的年,月,日
17         int nowYear = now.Year;
18         int nowMonth = now.Month;
19         int nowDay = now.Day;
20 
21         //得到天
22         if (nowDay >= birthDay)
23         {
24             day = nowDay - birthDay;
25         }
26         else
27         {
28             nowMonth -= 1;
29             day = GetDay(nowMonth, nowYear) + nowDay - birthDay;
30         }
31 
32         //得到月
33         if (nowMonth >= birthMonth)
34         {
35             month = nowMonth - birthMonth;
36         }
37         else
38         {
39             nowYear -= 1;
40             month = 12 + nowMonth - birthMonth;
41         }
42 
43         //得到年
44         year = nowYear - birthYear;
45     }
46 
47     /// <summary>
48     /// 获取天数
49     /// </summary>
50     private int GetDay(int month, int year)
51     {
52         int day = 0;
53         switch (month)
54         {
55             case 1:
56             case 3:
57             case 5:
58             case 7:
59             case 8:
60             case 10:
61             case 12:
62                 day = 31;
63                 break;
64             case 2:
65                 //闰年天,平年天
66                 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
67                 {
68                     day = 29;
69                 }
70                 else
71                 {
72                     day = 28;
73                 }
74                 break;
75             case 4:
76             case 6:
77             case 9:
78             case 11:
79                 day = 30;
80                 break;
81         }
82         return day;
83     }

 Js代码:

 1 function GetDateValue(entry){
 2             //差值
 3             var year;
 4             var month;
 5             var day;
 6 
 7             //入职日期
 8             var dateArr = entry.split("-");
 9             var entry_year = parseInt(dateArr[0]);
10             var entry_month = parseInt(dateArr[1]);
11             var entry_day = parseInt(dateArr[2]);
12             //当前日期
13             var d = new Date();
14             var now_year = d.getFullYear();
15             var now_day = d.getDate();
16             var now_month = d.getMonth() + 1;
17 
18             //得到天
19             if(now_day >= entry_day){
20                 day = now_day - entry_day;
21             }else{
22                 now_month = now_month - 1;
23                 day = GetDay(now_month,now_year) + now_day - entry_day;
24             }
25             //得到月
26             if(now_month >= entry_month){
27                 month = now_month - entry_month;
28             }else{
29                 now_year = now_year -1;
30                 month = 12 + now_month - entry_month;
31             }
32             //得到年
33             year = now_year - entry_year;
34 
35             //字符串
36             var basic_experience = " 在职";
37             if (year != 0)
38                 basic_experience += Math.abs(year) + "年";
39             if (month != 0)
40                 basic_experience += Math.abs(month) + "月";
41             if (day != 0)
42                 basic_experience += Math.abs(day) + "天";
43             return basic_experience;
44         }
45         //获取天
46         function GetDay(month,year){
47             var day = 0;
48             if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
49                 day = 31;
50             }else if(month == 2){
51                 if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){ 
52                     day = 29;
53                 }else{
54                     day = 28;
55                 }
56             }else if(month == 4 || month == 6 || month == 9 || month == 11){
57                 day = 30;
58             }
59             return day;
60         }

 

posted @ 2015-01-19 16:42  Wangyk  阅读(765)  评论(0编辑  收藏  举报