-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.html
150 lines (122 loc) · 2.83 KB
/
calendar.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
body {
background: #e0e0e0;
}
#cal {
width: 280px;
}
.header {
background-color: red;
color: white;
height: 30px;
line-height: 30px;
text-align: center;
border-radius: inherit;
text-transform: uppercase;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
#dates {
border-collapse: collapse;
}
td {
width: 40px;
height: 40px;
text-align: center;
cursor: default;
}
#dates thead td:first-child {
border-left: 1px solid #e6e6e6;
}
#dates thead td:last-child {
border-right: 1px solid #e6e6e6;
}
#dates tbody td {
border: 1px solid #e6e6e6;
text-transform: uppercase;
}
#dates tbody td:not(.nil):hover {
background-color: red;
}
.today {
background-color: gray;
}
</style>
</head>
<body>
<div id="cal">
<div class="header"><span class="curMonYear">March 2017</span></div>
<table id="dates">
<thead id="days">
<tr><td>SUN</td><td>MON</td><td>TUE</td><td>WED</td><td>THU</td><td>FRI</td><td>SAT</td></tr>
</thead>
<tbody id="calFrame">
</tbody>
</table>
</div>
<script>
function CALENDAR(){
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const numCells = 35;
var cache = {};
function init(){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
createCal(year, month);
}
function createCal(year, month){
var tableContent = '';
if(cache.hasOwnProperty(year) && cache[year].hasOwnProperty(month) ){
tableContent = cache[year][month];
}
else{
const totalDays = [31, (year%400 === 0 || (year%100 !== 0 && year%4 === 0)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var calFrame = document.getElementById('calFrame');
var date = new Date();
var today = date.getDate();
// Show month and year
document.querySelector('.curMonYear').innerHTML = months[month] + ' ' + year;
// Show the dates;
// Decide the day of the first day of the month
var firstDate = new Date(year, month);
var firstDay = firstDate.getDay(); // which day of the week
var currDay = 1;
for(let i = 0; i < numCells; i++){
if(i % 7 === 0){
tableContent += '<tr>';
}
if(firstDay <= i && i < firstDay + totalDays[month] ){
tableContent += '<td' + (firstDay + today - 1 === i ? ' class="today"' : '') + '>'+ (i - firstDay + 1) + '</td>';
}
else {
tableContent += '<td class="nil"> </td>';
}
if(i % 7 === 6){
tableContent += '</tr>';
}
}
if( !cache.hasOwnProperty(year) ){
cache[year] = {};
}
cache[year][month] = tableContent;
}
calFrame.innerHTML = tableContent;
}
return {
init: init
};
}
window.onload = function() {
var calendar = CALENDAR();
calendar.init();
}
</script>
</body>
</html>