-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
144 lines (136 loc) · 4.65 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
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
const getFebDays = (year) => (
(year % 4 == 0 && year % 100 != 0 && year % 400 != 0) ||
(year % 100 == 0 && year % 400 == 0)
) ? 29 : 28;
let calendar = document.querySelector('.calendar');
const month_names = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
let month_picker = document.querySelector('#month-picker');
const timeFormate = document.querySelector('.time-formate');
const dateFormate = document.querySelector('.date-formate');
const dayshow = document.querySelector('.date-time-formate');
const foot = document.querySelector('.calendar-footer');
const height = screen.availHeight - calendar.offsetTop * 2 - 30 - 90;
month_picker.onclick = () => {
month_list.classList.remove('hideonce');
month_list.classList.remove('hide');
month_list.classList.add('show');
timeFormate.classList.remove('showtime');
timeFormate.classList.add('hidetime');
dayshow.classList.remove('showtime');
dayshow.classList.add('hidetime');
dateFormate.classList.remove('showtime');
dateFormate.classList.add('hidetime');
};
const calendar_days = document.querySelector('.calendar-days');
const calendar_header_year = document.querySelector('#year');
const generateCalendar = (month,year) => {
calendar_days.innerHTML = '';
let days_of_month = [
31,
getFebDays(year),
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
month_picker.innerHTML = month_names[month];
calendar_header_year.innerHTML = year;
let first_day = new Date(year,month);
for(let j=days_of_month[month],i=1-first_day.getDay(),
w=[currentDate.getDate(),currentDate.getFullYear(),currentDate.getMonth()];
i<=j;i++){
let day = document.createElement('div');
if(i>0){
day.innerHTML = i;
if(i==w[0]&&year==w[1]&&month==w[2])day.classList.add('current-date');
}else{
day.classList.add('not-in-date');
}
calendar_days.appendChild(day);
}
foot.style.height = height - (calendar.scrollHeight - foot.scrollHeight) + 'px';//put generateCalendar() after month_names-209
};
let month_list = calendar.querySelector('.month-list');
document.querySelector('#pre-year').onclick = () => {
--currentYear.value;
generateCalendar(currentMonth.value, currentYear.value);
};
document.querySelector('#next-year').onclick = () => {
++currentYear.value;
generateCalendar(currentMonth.value, currentYear.value);
};
let currentDate = new Date();
let currentMonth = {value : currentDate.getMonth()};
let currentYear = {value : currentDate.getFullYear()};
document.getElementById('today').onclick = () => generateCalendar(currentDate.getMonth(), currentDate.getFullYear());
month_names.forEach((e, index) => {
let month = document.createElement('div');
month.innerHTML = `<div>${e}</div>`;
month_list.append(month);
month.onclick = () => {
currentMonth.value = index;
generateCalendar(currentMonth.value, currentYear.value);
month_list.classList.remove('show');
month_list.classList.add('hide');
timeFormate.classList.remove('hidetime');
timeFormate.classList.add('showtime');
dateFormate.classList.remove('hidetime');
dateFormate.classList.add('showtime');
dayshow.classList.remove('hidetime');
dayshow.classList.add('showtime');
};
});
month_list.classList.add('hideonce');
generateCalendar(currentMonth.value, currentYear.value);
function showtodate(){
const currshowDate = new Date();
const showCurrentDateOption = {
year : 'numeric',
month : 'long',
day : 'numeric',
weekday : 'long',
};
dateFormate.textContent = new Intl.DateTimeFormat(
'en-US',
showCurrentDateOption
).format(currshowDate);
return(currshowDate.getDate());
};
let thedate = showtodate();
setInterval(() => {
const timer = new Date();
const option = {
hour : 'numeric',
minute : 'numeric',
second : 'numeric',
};
const formateTimer = new Intl.DateTimeFormat('en-us', option).format(timer);
let time = `${`${timer.getHours()}`.padStart(
2,
'0'
)}:${`${timer.getMinutes()}`.padStart(
2,
'0'
)}:${`${timer.getSeconds()}`.padStart(2,'0')}`;
timeFormate.textContent = formateTimer;
if(thedate != timer.getDate()) thedate=showtodate();
},1000);