-
Notifications
You must be signed in to change notification settings - Fork 2
/
calendar.html
208 lines (183 loc) · 6.16 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<html>
<head>
<title>JavaScript calendar</title>
<script>
function GetEventDates(month) {
var events = {
"0": [21],
"1": [21],
"2": [25],
"3": [1,22],
"4": [21],
"5": [21],
"6": [21],
"7": [21],
"8": [21],
"9": [21],
"10": [21],
"11": [21],
};
return events[month];
}
function displayCalendar() {
var dateNow = new Date();
var month = dateNow.getMonth();
var year = dateNow.getFullYear();
renderCalendar(month, year);
}
function renderCalendar(monthToDisplay, yearToDisplay) {
var htmlContent = "";
var FebNumberOfDays = "";
var counter = 1;
var dateNow = new Date();
var month = monthToDisplay;
document.getElementById("currentMonth").value = month;
var nextMonth = month + 1;
var prevMonth = month - 1;
var day = dateNow.getDate();
var year = yearToDisplay;
document.getElementById("currentYear").value = year;
if (month == 1) {
if ((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0)) {
FebNumberOfDays = 29;
} else {
FebNumberOfDays = 28;
}
}
// names of months and week days.
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thrusday", "Friday", "Saturday"];
var dayPerMonth = ["31", "" + FebNumberOfDays + "", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"]
// days in previous month and next one , and day of week.
var nextDate = new Date(nextMonth + ' 1 ,' + year);
var weekdays = nextDate.getDay();
var weekdays2 = weekdays
var numOfDays = dayPerMonth[month];
htmlContent += "<ul class=\"days\">";
while (weekdays > 0) {
htmlContent += "<li> </li>";
weekdays--;
}
while (counter <= numOfDays) {
if (weekdays2 > 6) {
weekdays2 = 0;
//htmlContent += "</tr><tr>";
}
var dates = GetEventDates(month);
if (dates.indexOf(counter) != -1) {
htmlContent += "<li><span class=\"active\">" + counter + "</span></li>";
}
else {
htmlContent += "<li>" + counter + "</li>";
}
weekdays2++;
counter++;
}
htmlContent += "</ul>";
var calendarBody = "<div class=\"month\"><ul><li class=\"prev\"><a href=\"javascript:void(0)\" onclick=\"javascript:displayPrevMonth()\">❮</a></li><li class=\"next\"><a href=\"javascript:void(0)\" onclick=\"javascript:displayNextMonth()\">❯</a></li><li>" + monthNames[month] + " " + year + "</span></li></ul></div>"
calendarBody += "<ul class=\"weekdays\">";
for (i = 0; i < dayNames.length; i++) {
calendarBody += "<li>" + dayNames[i] + "</li>";
}
calendarBody += "</ul>";
calendarBody += htmlContent;
document.getElementById("calendar").innerHTML = calendarBody;
}
function displayNextMonth() {
var month = parseInt(document.getElementById("currentMonth").value);
var year = parseInt(document.getElementById("currentYear").value);
month++;
if (month > 11) {
year++;
month = 0;
}
renderCalendar(month, year);
}
function displayPrevMonth() {
var month = parseInt(document.getElementById("currentMonth").value);
var year = parseInt(document.getElementById("currentYear").value);
month--;
if (month < 0) {
year--;
month = 11;
}
renderCalendar(month, year);
}
</script>
</head>
<body onload="displayCalendar()">
<div id="calendar"></div>
<input type="hidden" id="currentMonth" />
<input type="hidden" id="currentYear" />
</body>
<style>
/* NEW */
ul {
list-style-type: none;
}
body {
font-family: Verdana, sans-serif;
}
/* Month header */
li a {
text-decoration: none;
color: black;
}
.month {
background: #1abc9c;
padding: 10px 0;
}
/* Month list */
.month ul {
margin: 0;
padding: 0;
}
.month ul li {
color: white;
font-size: 20px;
text-transform: uppercase;
letter-spacing: 3px;
text-align: center;
}
/* Previous button inside month header */
.month .prev {
float: left;
}
/* Next button */
.month .next {
float: right;
}
/* Weekdays (Mon-Sun) */
.weekdays {
margin: 0;
padding: 10px 0;
background-color: #ddd;
}
.weekdays li {
display: inline-block;
width: 13.6%;
color: #666;
text-align: center;
}
/* Days (1-31) */
.days {
padding: 10px 0;
background: #eee;
margin: 0;
}
.days li {
list-style-type: none;
display: inline-block;
width: 13.6%;
text-align: center;
margin-bottom: 5px;
color: #777;
}
/* Highlight the "current" day */
.days li .active {
padding: 5px;
background: #1abc9c;
color: white !important
}
</style>
</html>