-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.js
More file actions
86 lines (65 loc) · 2.46 KB
/
calendar.js
File metadata and controls
86 lines (65 loc) · 2.46 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
"use strict";
/* Set the date displayed in the calender */
var thisDay = new Date();
/* Write the calendar to the element with the id "calendar" */
document.getElementById("newCalendar").innerHTML = createCalendar(thisDay);
//function to generate the calendar table
function createCalendar(calDate) {
var calendarHTML = "<span class='b'><table id='calendar_table'>";
calendarHTML += calCaption(calDate);
calendarHTML += calWeekdayRow();
calendarHTML += calDays(calDate);
calendarHTML += "</table> </spam>";
return calendarHTML;
}
function calCaption(calDate) {
var monthName = ["January", "february", "march", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
//determine the curent month
var thisMonth = calDate.getMonth();
//Determine the current year
var thisYear = calDate.getFullYear();
// Write the caption
return "<caption>" + monthName[thisMonth] + " " + thisYear + "</caption>";
}
function calWeekdayRow() {
var dayName = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
var rowHTML = "<tr>";
for (var i = 0; i < dayName.length; i++) {
rowHTML += "<th class='calendar_weekdays'>" + dayName[i] + "</th>";
}
rowHTML += "</tr>";
return rowHTML;
}
function daysInMonth(calDate) {
var dayCount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var thisYear = calDate.getFullYear();
var thisMonth = calDate.getMonth();
if (thisYear % 4 === 0) {
if ((thisYear % 100 != 0) || (thisYear % 400 === 0)) {
dayCount[1] = 29;
}
}
return dayCount[thisMonth];
}
function calDays(calDate) {
var day = new Date(calDate.getFullYear(), calDate.getMonth(), 1);
var weekDay = day.getDay();
var htmlCode = "<tr>";
for (var i = 0; i < weekDay; i++){
htmlCode += "<td></td>";
}
var totalDays = daysInMonth(calDate);
var highlightDay = calDate.getDate();
for (i = 1; i <= totalDays; i++){
day.setDate(i);
weekDay = day.getDay();
if (weekDay === 0) htmlCode += "<tr>";
if(i === highlightDay){
htmlCode += "<td class='calendar_dates' id='calendar_today'>" + i + "</td>";
} else {
htmlCode += "<td class='calendar_dates'>" + i + "</td>";
}
if (weekDay === 6) htmlCode += "</tr>";
}
return htmlCode;
}