【コーディング】カレンダー

作成: Calc Page(CodePen)

<div id="calendar">
<div class="calendar">
  <div class="calendar-header">
    <button id="prevMonth">‹</button>
    <h2 id="monthLabel"></h2>
    <button id="nextMonth">›</button>
  </div>

  <div class="calendar-week">
    <div class="sun">日</div>
    <div>月</div>
    <div>火</div>
    <div>水</div>
    <div>木</div>
    <div>金</div>
    <div class="sat">土</div>
  </div>

  <div class="calendar-days" id="calendarDays"></div>
</div>
</div>

$main: #2196f3;
$bg: #f3f4f6;
$today: #fde68a;

body {
  background: $bg;
  font-family: -apple-system
    , BlinkMacSystemFont, sans-serif;
  padding: 10px;
}

#calendar {
  display: flex;
  justify-content: center;
  align-items: center;
}

.calendar {
  width: 360px;
  margin: auto;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: $main;
  color: #fff;
  padding: 12px 16px;

  button {
    background: none;
    border: none;
    color: #fff;
    font-size: 24px;
    cursor: pointer;
  }

  h2 {
    font-size: 16px;
  }
}

.calendar-week,
.calendar-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}

.calendar-week div {
  text-align: center;
  padding: 8px 0;
  font-size: 12px;
  color: #6b7280;
}

.calendar-days {
  grid-auto-rows: 48px;
}

.calendar-days div {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 2px;
  border-radius: 8px;
  font-size: 14px;
}

.today {
  background: $today;
  font-weight: bold;
}

.sun {
  color: #ef4444;
}

.sat {
  color: #3b82f6;
}

.holiday {
  background: #fee2e2;
  color: #b91c1c;
  font-weight: bold;
}

.other-month {
  color: #d1d5db;
}

$(function () {

  let currentDate = new Date();

  // 2026年日本の祝日(固定データ)
  const holidays2026 = {
    "2026-01-01": "元日",
    "2026-01-12": "成人の日",
    "2026-02-11": "建国記念の日",
    "2026-02-23": "天皇誕生日",
    "2026-03-20": "春分の日",
    "2026-04-29": "昭和の日",
    "2026-05-03": "憲法記念日",
    "2026-05-04": "みどりの日",
    "2026-05-05": "こどもの日",
    "2026-07-20": "海の日",
    "2026-08-11": "山の日",
    "2026-09-21": "敬老の日",
    "2026-09-22": "秋分の日",
    "2026-10-12": "体育の日",
    "2026-11-03": "文化の日",
    "2026-11-23": "勤労感謝の日"
  };

  function renderCalendar(date) {
    const year = date.getFullYear();
    const month = date.getMonth();
    const today = new Date();

    $("#monthLabel")
      .text(year + "年 " + (month + 1) + "月");

    const firstDay = 
          new Date(year, month, 1).getDay();
    const lastDate = 
          new Date(year, month + 1, 0).getDate();
    const prevLastDate = 
          new Date(year, month, 0).getDate();

    let html = "";

    // 前月分
    for (let i = firstDay - 1; i >= 0; i--) {
      html += '<div class="other-month">' 
           + (prevLastDate - i) + " </div>";
    }

    // 今月分
    for (let d = 1; d <= lastDate; d++) {
      const dayOfWeek = 
            new Date(year, month, d).getDay();
      const dateStr = year + "-" +
        String(month + 1).padStart(2, "0") + "-" +
        String(d).padStart(2, "0");

      let classes = [];

      if (
        year === today.getFullYear() &&
        month === today.getMonth() &&
        d === today.getDate()
      ) {
        classes.push("today");
      }

      if (dayOfWeek === 0) classes
        .push("sun");
      if (dayOfWeek === 6) classes
        .push("sat");
      if (holidays2026[dateStr]) classes
        .push("holiday");

      html += '<div class="' + classes.join(" ") 
        + '" title="' +
        (holidays2026[dateStr] || "") + '">' + d + "</div>";
    }

    // 翌月分(42マスまで)
    const total = firstDay + lastDate;
    for (let i = 1; i <= 42 - total; i++) {
      html += '<div class="other-month">' + i + "</div>";
    }

    $("#calendarDays").html(html);
  }

  renderCalendar(currentDate);

  $("#prevMonth").on("click", function () {
    currentDate.setMonth(currentDate.getMonth() - 1);
    renderCalendar(currentDate);
  });

  $("#nextMonth").on("click", function () {
    currentDate.setMonth(currentDate.getMonth() + 1);
    renderCalendar(currentDate);
  });

});
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次