-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (80 loc) · 2.98 KB
/
script.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
var calendarLibarary = function() {
var prevYear = document.getElementById('prevYear');
var prevMonth = document.getElementById('prevMonth');
var nextYear = document.getElementById('nextYear');
var nextMonth = document.getElementById('nextMonth');
var todayButton = document.getElementById('today');
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var todayDate = today.getDate();
var todayMonth = today.getMonth();
var todayYear = today.getFullYear();
var year;
var month;
function printCalendar(year, month) {
var cols = 6;
var currentDate = new Date(year, month, 1);
var currentDateDay = currentDate.getDay();
//start from last monday of previous month
currentDate.setDate(currentDate.getDate()- currentDateDay);
var cells = document.querySelectorAll('tbody tr td');
for(var i = 0; i < 7; i++){
var head = document.getElementById('head')
head.innerText = months[month] + ', ' + year;
for(var j = 0; j < cols; j++){
if(currentDate.getMonth() == todayMonth && currentDate.getFullYear() == todayYear && currentDate.getDate() == todayDate){
cells[(i * cols) + j].style['font-weight'] = 'bold';
cells[(i * cols) + j].style['border'] = '1px solid black';
cells[(i * cols) + j].style['background-color'] = '#fcf8e3';
}
//default values
else{
cells[(i * cols) + j].style.color = '';
cells[(i * cols) + j].style['border'] = '';
cells[(i * cols) + j].style['font-weight'] = '';
cells[(i * cols) + j].style['background-color'] = '';
}
if (currentDate.getMonth() != month) {
cells[(i * cols) + j].style.color = 'lightgrey';
}
cells[(i * cols) + j].innerText = currentDate.getDate();
currentDate.setDate(currentDate.getDate() + 1);
}
}
}
function prevMonthHelper(){
today.setMonth(today.getMonth() - 1);
month = today.getMonth();
year = today.getFullYear()
printCalendar(year, month);
}
function prevYearHelper(){
today.setYear(today.getFullYear() - 1);
console.log(today.getMonth());
month = today.getMonth();
year = today.getFullYear()
printCalendar(year, month);
}
function nextMonthHelper(){
today.setMonth(today.getMonth() + 1);
month = today.getMonth();
year = today.getFullYear()
printCalendar(year, month);
}
function nextYearHelper(){
today.setYear(today.getFullYear() + 1);
month = today.getMonth();
year = today.getFullYear();
printCalendar(year, month);
}
function todayHelper(){
printCalendar(todayYear, todayMonth);
}
printCalendar(today.getFullYear(), today.getMonth());
prevMonth.addEventListener('click', prevMonthHelper);
nextMonth.addEventListener('click', nextMonthHelper);
prevYear.addEventListener('click', prevYearHelper);
nextYear.addEventListener('click', nextYearHelper);
todayButton.addEventListener('click', todayHelper);
}
calendarLibarary();