-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
215 lines (173 loc) · 6.05 KB
/
calendar.js
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
209
210
211
212
213
214
215
/*
File: JS for creating calendar
Author: Anh Uong
Date: July 22, 2015
*/
// builds mini calendar, showing
// starts building the table, adding the Month Year header and weekdays
function makeHeader(mmyyyy) {
var table = document.getElementById('calendar');
/* gets rid of previous elements in table */
for( var i=table.childNodes.length-1; i>0; i--){
table.removeChild(table.childNodes[i]);
}
// creates header for table
// adds title (Month Year) and arrows
var row = document.createElement('TR');
var row2 = document.createElement('TR');
var title = document.createElement('TH');
var left = document.createElement('TH');
var right = document.createElement('TH');
var lefter = document.createElement('TH');
var righter = document.createElement('TH');
var date = mmyyyy.toDateString().slice(4, 8);
date += mmyyyy.getFullYear();
title.appendChild(document.createTextNode(date));
left.appendChild(document.createTextNode("<"));
right.appendChild(document.createTextNode(">"));
lefter.appendChild(document.createTextNode("<<"));
righter.appendChild(document.createTextNode(">>"));
row.appendChild(lefter);
row.appendChild(left);
row.appendChild(title);
row.appendChild(right);
row.appendChild(righter);
title.height = "30px";
title.style.fontWeight = "bold";
title.style.fontSize = "18px";
title.setAttribute("colspan", 3);
// adds ability to change months
left.onclick = function() {
changeMonth(mmyyyy, "L");
}
right.onclick = function() {
changeMonth(mmyyyy, "R");
}
lefter.onclick = function() {
changeYear(mmyyyy, "L");
}
righter.onclick = function() {
changeYear(mmyyyy, "R");
}
left.style.cursor = "pointer"
right.style.cursor = "pointer"
lefter.style.cursor = "pointer"
righter.style.cursor = "pointer"
// adds weekdays to second row of header
var weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
for( var i=0; i<weekdays.length; i++) {
var day = document.createElement('TH');
day.appendChild(document.createTextNode(weekdays[i]));
row2.appendChild(day);
day.style.fontWeight = "bold";
}
table.appendChild(row);
table.appendChild(row2);
}
// determines how many days are in the month
function numberOfDays(month, year) {
// if April, June, September, November -> 30 days
if (month == 3 || month == 5 || month == 8 || month == 10) {
return 30;
}
// if February, see if is leap year
else if (month == 1) {
if (year % 4 == 0) {
return 29;
}
else { return 28; }
}
// otherwise, 31 days
else {
return 31;
}
}
// creates the first row of calendar given the month and year
// depends on what weekday the first of month is
function makeFirstRow(date) {
date.setDate(1)
// determines what weekday for first of month
var DayofOne = date.toDateString().slice(0, 3);
var weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var DaytoDisplay = 1;
var DayOneHit = false;
var week = document.createElement("TR");
var month = document.getElementById("calendar");
// fills in the first row with blank or number
for( var i=0; i<7; i++) {
var day = document.createElement("TD");
// once hit DayofOne, start displying numbers
if (DayofOne == weekdays[i] || DayOneHit == true) {
day.appendChild( document.createTextNode(DaytoDisplay) );
day.style.cursor = "pointer";
DayOneHit = true;
DaytoDisplay++;
}
// else-fill with blank text
else {
day.appendChild( document.createTextNode( " ") );
}
week.appendChild(day);
}
month.appendChild(week);
return DaytoDisplay;
}
// puts all the pieces of the calendar together
function makeCalendar(mmyyy) {
// creates date based off of today or if change date
if (mmyyy == undefined){
var date = new Date();
}
else {
var date = mmyyy;
}
var title = date.toDateString().slice(4, 8);
title += date.getFullYear();
// builds initial calendar top
makeHeader(date);
var DaysinMonth = numberOfDays(date.getMonth(), date.getFullYear() );
var DaytoDisplay = makeFirstRow(date);
var month = document.getElementById("calendar");
// builds the rest of the rows of calendar
for (var i=0; i<5; i++){
var week = document.createElement("TR");
for (var j=0; j<7; j++) {
var day = document.createElement("TD");
if (DaytoDisplay > DaysinMonth) {
day.appendChild( document.createTextNode( " ") );
}
else {
day.appendChild( document.createTextNode( DaytoDisplay) );
day.style.cursor = "pointer";
DaytoDisplay++;
}
week.appendChild(day);
}
month.appendChild(week);
}
}
// when you click on one of the arrows, changes the month
// finds what month calendar is on and progresses
function changeMonth(date, direction) {
// var date = new Date(mmyyyy);
if (direction == "L") {
date.setMonth( date.getMonth() - 1);
}
else {
date.setMonth( date.getMonth() + 1);
}
makeCalendar(date)
}
// when you click on the double arrows, changes the year
// finds what year calendar is on and progresses
function changeYear(date, direction) {
// var date = new Date(mmyyyy);
if (direction == "L") {
date.setFullYear( date.getFullYear() - 1);
}
else {
date.setFullYear( date.getFullYear() + 1);
}
makeCalendar(date)
}
makeCalendar();