-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekchk.html
187 lines (160 loc) · 6.08 KB
/
weekchk.html
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
<!DOCTYPE html>
<html>
<head>
<title>Jaewoo's quick calendar</title>
<style>
h1 {text-align: center;}
h2 {text-align: center;}
div {text-align: center;}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 5px;
text-align: center;
}
.week-number {
font-weight: bold;
color: blue;
}
.today {
font-weight: bold;
color: red;
}
.calendar-table {
display: inline-block;
margin-right: 20px;
margin-bottom: 20px;
}
.message-box {
border: 1px solid black;
padding: 10px;
width: 200px;
text-align: center;
}
</style>
<script>
function getWeather() {
// Make a request to the weather API to get the weather data
// Replace 'YOUR_API_KEY' with your actual API key
fetch('https://api.weatherapi.com/v1/current.json?key=ad8caecea06c43d1912181938230407&q=Gothenburg')
.then(response => response.json())
.then(data => {
// Get the weather icon code from the API response
var weatherIcon = data.current.condition.icon;
// Create an <img> element for the weather icon
var weatherImg = document.createElement("img");
weatherImg.src = "https:" + weatherIcon;
// Append the weather icon to the weatherContainer div
var weatherContainer = document.getElementById("weatherContainer");
weatherContainer.appendChild(weatherImg);
});
}
// Function to get the current date and time
function getCurrentDateTime() {
var now = new Date();
// Create a formatted string for the date and time
var dateString = now.toDateString();
var timeString = now.toLocaleTimeString();
// Set the date and time in the page
var dateElement = document.getElementById("currentDate");
var timeElement = document.getElementById("currentTime");
dateElement.textContent = dateString;
timeElement.textContent = timeString;
}
function generateCalendar() {
var yearInput = document.getElementById("year");
var year = parseInt(yearInput.value);
var calendarContainer = document.getElementById("calendarContainer");
calendarContainer.innerHTML = "";
for (var month = 1; month <= 12; month++) {
var date = new Date(year, month - 1, 1);
var monthName = date.toLocaleString('default', { month: 'long' });
var daysInMonth = new Date(year, month, 0).getDate();
var calendarTable = document.createElement("table");
calendarTable.classList.add("calendar-table");
var thead = document.createElement("thead");
var tbody = document.createElement("tbody");
var monthTitleRow = document.createElement("tr");
var monthTitleCell = document.createElement("th");
monthTitleCell.textContent = monthName;
monthTitleCell.colSpan = 8;
monthTitleRow.appendChild(monthTitleCell);
thead.appendChild(monthTitleRow);
var headerRow = document.createElement("tr");
var weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "WN"];
weekdays.forEach(function(weekday) {
var th = document.createElement("th");
th.textContent = weekday;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
var firstDayOfMonth = new Date(year, month - 1, 1);
var startingWeekNumber = getISOWeekNumber(firstDayOfMonth); // Get the ISO week number of the first day in the month
var dayOfWeek = firstDayOfMonth.getDay();
if (dayOfWeek === 0) {
dayOfWeek = 7; // Adjust Sunday to be the last day of the week (7) instead of the first (0)
}
var week = startingWeekNumber;
var day = 1;
while (day <= daysInMonth) {
var weekRow = document.createElement("tr");
for (var weekday = 1; weekday <= 7; weekday++) {
var td = document.createElement("td");
if (weekday < dayOfWeek || day > daysInMonth) {
td.textContent = "";
} else {
td.textContent = day;
if (year === new Date().getFullYear() && month === new Date().getMonth() + 1 && day === new Date().getDate()) {
td.classList.add("today"); // Add the class for styling today's date
}
day++;
}
weekRow.appendChild(td);
}
var weekTd = document.createElement("td");
weekTd.classList.add("week-number"); // Add the class for styling
weekTd.textContent = week;
weekRow.appendChild(weekTd);
tbody.appendChild(weekRow);
// Increment the week number and adjust for the start of the next week
if (day <= daysInMonth) {
var nextWeekDate = new Date(year, month - 1, day);
week = getISOWeekNumber(nextWeekDate);
dayOfWeek = nextWeekDate.getDay();
if (dayOfWeek === 0) {
dayOfWeek = 7; // Adjust Sunday to be the last day of the week (7) instead of the first (0)
}
}
}
calendarTable.appendChild(thead);
calendarTable.appendChild(tbody);
calendarContainer.appendChild(calendarTable);
}
}
// Get ISO week number
function getISOWeekNumber(date) {
var startOfWeek = new Date(date);
startOfWeek.setDate(date.getDate() - date.getDay() + 1); // Set the date to the first day of the week (Monday)
var yearStart = new Date(startOfWeek.getFullYear(), 0, 1);
var weekNumber = Math.ceil(((startOfWeek - yearStart) / 86400000 + 1) / 7);
return weekNumber;
}
</script>
</head>
<body>
<h1 id="currentDate"></h1>
<h2 id="currentTime"></h2>
<div id="weatherContainer">Göteborgsvädret<br></div>
<center>
<label for="year">Select Year:</label><input type="number" id="year" value="2023"><button onclick="generateCalendar()">Go</button>
</center>
<div id="calendarContainer"></div>
<script>
getCurrentDateTime();
getWeather();
generateCalendar();
</script>
</body>
</html>