calendar.js
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* 星潮教室 亲密度·日期
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/4/11
*/
var $ = require('jquery');
var CalendarHandler = {
currentYear: 0,
currentMonth: 0,
initialize: function() {
var $calendarItem;
$calendarItem = this.createCalendar(0, 0);
$('#Container').append($calendarItem);
},
isRuiYear: function(aDate) {
return 0 === aDate % 4 && (aDate % 100 !== 0 || aDate % 400 === 0);
},
calculateWeek: function(y, m, d) {
var arr, vDay, week;
arr = '7123456'.split('');
vDay = parseInt(d, 10);
week = arr[new Date(y, m - 1, vDay).getDay()];
return week;
},
calculateMonthDays: function(m, y) {
var mDay = 0;
var isRn;
if (m === 0 || m === 1 || m === 3 || m === 5 || m === 7 || m === 8 || m === 10 || m === 12) {
mDay = 31;
} else {
if (m === 2) {
// 判断是否为芮年
isRn = this.isRuiYear(y);
if (isRn === true) {
mDay = 29;
} else {
mDay = 28;
}
} else {
mDay = 30;
}
}
return mDay;
},
createCalendar: function(y, m) {
var $dayItem, nowDate, nowYear, nowMonth, nowDaysNub, nowWeek, lastMonthDaysNub, i, hasCreateDaysNub;
var months = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November', 'December'];
// var signDay = $('.sign-day').val();
$dayItem = $('<div class="dayItem"></div>');
// 获取当前月份的天数
nowDate = new Date();
nowYear = y === 0 ? nowDate.getFullYear() : y;
this.currentYear = nowYear;
nowMonth = m === 0 ? nowDate.getMonth() + 1 : m;
this.currentMonth = nowMonth;
nowDaysNub = this.calculateMonthDays(nowMonth, nowYear);
window.nowDaysNub = nowDaysNub;
$('.cur-year').html(nowYear);
$('.cur-month').html(months[nowMonth - 1]);
// 获取当月第一天是星期几
nowWeek = parseInt(this.calculateWeek(nowYear, nowMonth, 1));
// 获取上个月的天数
lastMonthDaysNub = this.calculateMonthDays((nowMonth - 1), nowYear);
if (nowWeek !== 0) {
// 生成上月剩下的日期
for (i = (lastMonthDaysNub - nowWeek); i < lastMonthDaysNub; i++) {
$dayItem.append('<div class="item lastItem">' + (i + 1) + '</div>');
}
}
// 生成当月的日期
for (i = 1; i <= nowDaysNub; i++) {
// if (signDay.indexOf(i) > -1) {
// $dayItem.append('<div class="item signItem">' + i + '</div>');
// } else {
// $dayItem.append('<div class="item">' + i + '</div>');
// }
$dayItem.append('<div class="item now-days">' + i + '</div>');
}
// 获取总共已经生成的天数
hasCreateDaysNub = nowWeek + nowDaysNub;
// 如果小于42,往下个月推算
if (hasCreateDaysNub < 41) {
for (i = 0; i <= (41 - hasCreateDaysNub); i++) {
$dayItem.append('<div class="item lastItem">' + (i + 1) + '</div>');
}
}
return $dayItem;
}
};
$(document).ready(function() {
CalendarHandler.initialize();
});
exports.calculateMonthDays = CalendarHandler.calculateMonthDays;