Last active
October 26, 2022 04:12
-
-
Save kawahara-kunio/074fdf1f4b0fd26d9932b3f3724ad216 to your computer and use it in GitHub Desktop.
Calendar by JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" > | |
<head> | |
<meta charset="UTF-8"> | |
<title>JavaScript Calendar 5</title> | |
<link rel="stylesheet" href="./style.css"> | |
</head> | |
<body> | |
<!-- partial:index.partial.html --> | |
<button id="prev" type="button">前の月</button> | |
<button id="next" type="button">次の月</button> | |
<div id="calendar"></div> | |
<!-- partial --> | |
<script src="./script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2021 by daichi (https://codepen.io/kandai/pen/xmOgyz) | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const weeks = ["日", "月", "火", "水", "木", "金", "土"]; | |
const holidays = [ | |
// 祝日 | |
"2021/09/20", | |
"2021/09/23", | |
"2021/11/03", | |
"2021/11/23", | |
"2022/01/01", | |
"2022/01/10", | |
"2022/02/11", | |
"2022/03/21", | |
"2022/04/29", | |
"2022/05/03", | |
"2022/05/04", | |
"2022/05/05", | |
"2022/07/18", | |
"2022/08/11", | |
"2022/09/19", | |
"2022/09/23", | |
"2022/10/10", | |
"2022/11/03", | |
"2022/11/23", | |
"2023/01/01", | |
"2023/01/02", | |
"2023/01/09", | |
"2023/02/11", | |
"2023/02/23", | |
"2023/03/21", | |
"2023/04/29", | |
"2023/05/03", | |
"2023/05/04", | |
"2023/05/05", | |
"2023/07/17", | |
"2023/08/11", | |
"2023/09/18", | |
"2023/09/23", | |
"2023/10/09", | |
"2023/11/03", | |
"2023/11/23", | |
]; | |
const holidaysDate = holidays.map((holiday) => new Date(holiday)); | |
const config = { | |
displayMonthNumber: 5, // 表示する月の数 | |
}; | |
const date = new Date(); | |
let year = date.getFullYear(); | |
let month = date.getMonth() + 1; | |
function showCalendar(year, month) { | |
month = month - 2; // 2つ前の月から表示する | |
if (month < 1) { | |
year--; | |
month = 12; | |
} | |
for (i = 0; i < config.displayMonthNumber; i++) { | |
const calendarHtml = createCalendar(year, month); | |
const sec = document.createElement("section"); | |
sec.innerHTML = calendarHtml; | |
document.querySelector("#calendar").appendChild(sec); | |
month++; | |
if (month > 12) { | |
year++; | |
month = 1; | |
} | |
} | |
} | |
function createCalendar(year, month) { | |
const startDate = new Date(year, month - 1, 1); // 月の最初の日を取得 | |
const endDate = new Date(year, month, 0); // 月の最後の日を取得 | |
const endDayCount = endDate.getDate(); // 月の末日 | |
const lastMonthEndDate = new Date(year, month - 1, 0); // 前月の最後の日の情報 | |
const lastMonthEndDayCount = lastMonthEndDate.getDate(); // 前月の末日 | |
const startDay = startDate.getDay(); // 月の最初の日の曜日を取得 | |
let dayCount = 1; // 日にちのカウント | |
let calendarHtml = ""; // HTMLを組み立てる変数 | |
calendarHtml += "<h1>" + year + "/" + month + "</h1>"; | |
calendarHtml += "<table>"; | |
// 曜日の行を作成 | |
for (let i = 0; i < weeks.length; i++) { | |
calendarHtml += "<td>" + weeks[i] + "</td>"; | |
} | |
for (let w = 0; w < 6; w++) { | |
calendarHtml += "<tr>"; | |
for (let d = 0; d < 7; d++) { | |
if (w == 0 && d < startDay) { | |
// 1行目で1日の曜日の前 | |
let num = lastMonthEndDayCount - startDay + d + 1; | |
calendarHtml += '<td class="is-disabled">' + num + "</td>"; | |
} else if (dayCount > endDayCount) { | |
// 末尾の日数を超えた | |
let num = dayCount - endDayCount; | |
calendarHtml += '<td class="is-disabled">' + num + "</td>"; | |
dayCount++; | |
} else { | |
let cssClass = ""; | |
if (date.getMonth() == month - 1 && date.getDate() == dayCount) { | |
cssClass += "today"; | |
// calendarHtml += `<td class="today"><b>${dayCount}</b></td>`; | |
} | |
if (isHoliday(year, month - 1, dayCount)) { | |
cssClass += " holiday"; | |
// calendarHtml += `<td class="calendar_td">${dayCount}</td>`; | |
} | |
calendarHtml += `<td class="${cssClass}">${dayCount}</td>`; | |
dayCount++; | |
} | |
} | |
calendarHtml += "</tr>"; | |
} | |
calendarHtml += "</table>"; | |
return calendarHtml; | |
} | |
function isHoliday(year, month, day) { | |
return holidaysDate.some((holiday) => { | |
return ( | |
holiday.getFullYear() == year && | |
holiday.getMonth() == month && | |
holiday.getDate() == day | |
); | |
}); | |
} | |
function moveCalendar(e) { | |
document.querySelector("#calendar").innerHTML = ""; | |
if (e.target.id === "prev") { | |
month--; | |
if (month < 1) { | |
year--; | |
month = 12; | |
} | |
} | |
if (e.target.id === "next") { | |
month++; | |
if (month > 12) { | |
year++; | |
month = 1; | |
} | |
} | |
showCalendar(year, month); | |
} | |
document.querySelector("#prev").addEventListener("click", moveCalendar); | |
document.querySelector("#next").addEventListener("click", moveCalendar); | |
showCalendar(year, month); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#calendar { | |
display: flex; | |
flex-wrap: wrap; | |
} | |
body { | |
font-family: sans-serif; | |
} | |
section { | |
margin: 10px; | |
} | |
h1 { | |
font-size: 18px; | |
} | |
table { | |
border-spacing: 0; | |
border-collapse: collapse; | |
} | |
td { | |
border: 1px solid #ddd; | |
padding: 5px; | |
text-align: center; | |
} | |
td:first-child { | |
color: red; | |
} | |
td:last-child { | |
color: royalblue; | |
} | |
td.is-disabled { | |
color: #ccc; | |
} | |
td.today { | |
font-size: x-large; | |
font-weight: bold; | |
} | |
td.holiday { | |
color: red; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment