startDom, yearDom, monthDom, dayDom, 均为计算日期的ID
endDom 为最终计算结果展示的ID
1 //根据合同期始计算合同期止 传入CalculationDate的参数均为ID
2 function CalculationDate(startDom, yearDom, monthDom, dayDom, endDom) {
3 var start_date = $(startDom).val().trim();
4 if (start_date) {
5 var year = parseInt(start_date.substring(0, 4)),
6 month = parseInt(start_date.substring(5, 7)),
7 day = parseInt(start_date.substring(8));
8 year = parseInt(year) + parseInt($(yearDom).val().trim());
9 month = parseInt(month) + parseInt($(monthDom).val().trim());
10 if ($(dayDom).val() == "") {
11 day = parseInt(day);
12 }
13 else {
14 day = parseInt(day) + parseInt($(dayDom).val().trim());
15 }
16 if (month > 12) {
17 month = month - 12;
18 year = year + 1;
19 }
20 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
21 if (day > 31) {
22 day = day - 31;
23 month = month + 1;
24 }
25 } else if (month == 4 || month == 6 || month == 9 || month == 11) {
26 if (day > 30) {
27 day = day - 30;
28 month = month + 1;
29 }
30 } else if (month == 2) {
31 if (isLeapYear(year)) {
32 if (day > 29) {
33 day = day - 29;
34 month = month + 1;
35 }
36 } else {
37 if (day > 28) {
38 day = day - 28;
39 month = month + 1;
40 }
41 }
42 }
43
44 if (day == 1) {
45 var newmonth = month - 1;
46 if (newmonth == 0) {
47 month = 12;
48 year--;
49 day = 31;
50 } else if (newmonth == 1 || newmonth == 3 || newmonth == 5 || newmonth == 7 || newmonth == 8 || newmonth == 10) {
51 month--;
52 day = 31;
53 } else if (newmonth == 4 || newmonth == 6 || newmonth == 9 || newmonth == 11) {
54 month--;
55 day = 30;
56 } else if (newmonth == 2) {
57 month--;
58 if (isLeapYear(year)) {
59 day = 29;
60 } else {
61 day = 28;
62 }
63 }
64 } else {
65 day--;
66 }
67 $(endDom).val(year + "/" + month + "/" + day);
68 }
69 }
70 //判断是否为闰年
71 function isLeapYear(year) {
72 return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
73 }