-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.js
More file actions
192 lines (163 loc) · 5.08 KB
/
calendar.js
File metadata and controls
192 lines (163 loc) · 5.08 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
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
window.onload = Calendar;
function Calendar() {
//All variables can be created in a single statement, but I broke them up for my ability to read it
var elCalendar = document.getElementById('calendar'),
elMonthOpt = document.getElementById('monthOpt'),
elYearOpt = document.getElementById('yearOpt');
//Month select and year input elements are created here to make available to multiple functions within script without passing as parameter
var monthSelect = document.createElement('select'),
monthlyCalendar = document.createElement('table'),
yearInput = document.createElement('input');
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
today = new Date();
//Functions called onload
addCalendar();
//All functions that append to page should be called here
function addCalendar() {
createMonthSelect();
createYearInput();
createUpdateButton();
createMonthlyCalendar(today.getMonth(), today.getFullYear());
}
function getDaysInMonth(month, year) {
var totalDays;
switch(month) {
//January, March, May, July, August, October, December
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
totalDays = 31;
break;
//April, June, September, November
case 3:
case 5:
case 8:
case 10:
totalDays = 30;
break;
//February: check if leap year
case 1:
if (isLeapYear) {
totalDays = 29;
}
else {
totalDays = 28;
};
break;
};
return totalDays;
}//End getDaysInMonth
function isLeapYear(year) {
var leapYear;
if (year % 400 === 0) {
leapYear = true;
}
else if (year % 100 === 0) {
leapYear = false;
}
else if (year % 4 === 0) {
leapYear = true;
}
else {
leapYear = false;
};
return leapYear;
}//End isLeapYear
function getMonthName(i) {
return months[i];
}
function getDayName(i) {
return days[i];
}
//Returns the number of calendar weeks in requested month
function getWeeksInMonth (month, year) {
var numOfDays = getDaysInMonth(month, year),
firstDayIndex = new Date(year, month, 1).getDay();
//Always round up to account for incomplete weeks
return Math.ceil((numOfDays + firstDayIndex) / 7);
}
function createMonthlyCalendar (month, year) {
var firstDayIndex = new Date(year, month, 1).getDay(),
numOfWeeks = getWeeksInMonth(month, year),
numOfDays = getDaysInMonth(month, year);
//Clear our any prior calendar data
monthlyCalendar.innerHTML = '';
monthlyCalendar.id = 'monthlycalendar';
//Dependent on Bootstrap 3.0 for formatting
monthlyCalendar.setAttribute('class', 'table table-striped');
//Create row for each calendar week
var cells = [];
for (var i = 0; i < numOfWeeks; i++) {
var week = monthlyCalendar.insertRow(i);
for (var j = 0; j < 7; j++) {
var day = week.insertCell(j);
cells.push(day);
};
};
//x is equivalent of i - firstDayIndex; used for day part of date
var x = 1;
//Start looping at index of first day so that first day is put on correct day of week
for (var i = firstDayIndex; i < numOfDays + firstDayIndex; i++) {
//Assign x as date
cells[i].innerHTML = x;
cells[i].id = getDateId(x, month, year);
//Class is assigned here and not when cells are created to avoid tagging unfilled cells
cells[i].setAttribute('class', 'date');
x++;
};
//Create header row with day of week
var thead = monthlyCalendar.createTHead();
var headRow = thead.insertRow(0);
for (var i = 0; i < 7; i++) {
var day = headRow.insertCell(i);
day.innerHTML = days[i].toString().substring(0,2);
};
elCalendar.appendChild(monthlyCalendar);
}//End createMonthlyCalendar
function getDateId(day, month, year) {
return year + BelowTenFormat(month) + BelowTenFormat(day);
}
function createMonthSelect() {
//Select element created at beginning of script to be available to multiple functions without being passed as a parameter
for (var i = 0; i < 12; i++) {
var month = document.createElement('option');
month.text = months[i];
month.value = i;
monthSelect.add(month);
};
monthSelect.onchange = function() {
elCalendar.removeChild(monthlyCalendar);
createMonthlyCalendar(monthSelect.selectedIndex, yearInput.value);
};
monthSelect.value = today.getMonth();
elMonthOpt.appendChild(monthSelect);
}
function createYearInput() {
yearInput.value = today.getFullYear();
//Stlying changes are not necessary
yearInput.style.textAlign = 'center';
yearInput.style.width = '75px';
elYearOpt.appendChild(yearInput);
}
function createUpdateButton() {
var updateBtn = document.createElement('button');
//Bootstrap dependency
updateBtn.setAttribute('class', 'btn btn-primary btn-sm inline');
updateBtn.innerHTML = 'Update Year';
updateBtn.onclick = function() {
elCalendar.removeChild(monthlyCalendar);
createMonthlyCalendar(monthSelect.selectedIndex, yearInput.value);
};
elYearOpt.appendChild(updateBtn);
}
}
function BelowTenFormat(i) {
if (i < 10)
i = "0" + i;
return i;
};