๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

[JS] ๋™์ ์บ˜๋ฆฐ๋” ์†Œ์Šค์ฝ”๋“œ ๋ณธ๋ฌธ

Deveolpment Study๐Ÿ—‚๏ธ/Javascript

[JS] ๋™์ ์บ˜๋ฆฐ๋” ์†Œ์Šค์ฝ”๋“œ

bompeach 2024. 5. 13. 14:04
<div id="calendar-controls">
  <button id="prevMonth">โ—</button>
  <span id="currentYearMonth"></span>
  <button id="nextMonth">โ–ท</button>
</div>
<div id="calendar"></div>
<div id="time-slots" style="display:none;"> 
</div>
<div id="selectedDateTime"></div>
table {
  width: 400px;
  height: 343px;
  border-collapse: collapse;
  text-align: center;
}

th, td {
  border: 1px solid #ccc;
  padding: 8px;
  text-align: center;
}

th.day-name {  /* ์š”์ผ ํ‘œ์‹œ ๋ถ€๋ถ„์˜ ์Šคํƒ€์ผ */
  background-color: transparent;  /* ๋ฐฐ๊ฒฝ ํˆฌ๋ช… */
  border: none;  /* ํ…Œ๋‘๋ฆฌ ์ œ๊ฑฐ */
  color: #4a506b;  /* ๊ธ€์ž์ƒ‰ ํšŒ์ƒ‰ */
}

td.date-cell {  /* ๋‚ ์งœ ์…€ ์Šคํƒ€์ผ */
  cursor: pointer;  /* ์ปค์„œ๋ฅผ ํฌ์ธํ„ฐ๋กœ ์„ค์ • */
}

td.date-cell:hover {
  background-color:#586e9a;
  color:white;
}

button {
  padding: 10px;
  margin: 5px;
  background-color: #f9f9f9;
  border: none;
  cursor: pointer;
}

#calendar-controls {
  width: 400px;
  text-align: center;
  margin-bottom: 20px;
}

.selected {
  background-color: #e63c64;  // ์„ ํƒ๋œ ์…€์˜ ๋ฐฐ๊ฒฝ์ƒ‰
  color: white !;           // ์„ ํƒ๋œ ์…€์˜ ํ…์ŠคํŠธ ์ƒ‰์ƒ
}
window.onload = function() {
  const today = new Date();
  currentYear = today.getFullYear();
  currentMonth = today.getMonth() + 1;
  createCalendar(currentYear, currentMonth);

  document.getElementById('prevMonth').addEventListener('click', function() {
    changeMonth(-1);
  });

  document.getElementById('nextMonth').addEventListener('click', function() {
    changeMonth(1);
  });
};

function createCalendar(year, month) {
  const container = document.getElementById('calendar');
  container.innerHTML = '';
  const daysInMonth = new Date(year, month, 0).getDate();
  const firstDayOfMonth = new Date(year, month - 1, 1).getDay();
  const today = new Date(); // ์˜ค๋Š˜ ๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

  const table = document.createElement('table');
  const header = table.createTHead();
  const headerRow = header.insertRow(0);
  const dayNames = ['์ผ', '์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† '];

  dayNames.forEach(day => {
    const cell = document.createElement('th');
    cell.textContent = day;
    headerRow.appendChild(cell);
  });

  const calendarBody = table.createTBody();
  let date = 1;
  let usedRows = 0;

  for (let i = 0; i < 6; i++) { // ์ตœ๋Œ€ 6์ฃผ
    const row = calendarBody.insertRow();
    let rowHasDates = false;

    for (let j = 0; j < 7; j++) {
      const cell = row.insertCell();
      if (i === 0 && j < firstDayOfMonth) {
        cell.classList.add('empty');
      } else if (date > daysInMonth) {
        cell.classList.add('empty');
      } else {
        cell.textContent = date;
        cell.classList.add('date-cell');
        
        if (today.getFullYear() === year && today.getMonth() + 1 === month && today.getDate() === date) {
          cell.style.border = "3px solid #586e9a"; // ์˜ค๋Š˜ ๋‚ ์งœ์— ํ…Œ๋‘๋ฆฌ ์ถ”๊ฐ€
        }
        
        
        (function(currentYear, currentMonth, currentDate) {
          cell.addEventListener('click', function() {
            document.querySelectorAll('.date-cell').forEach(c => c.classList.remove('selected'));
            this.classList.add('selected');
            displaySelectedDate(currentYear, currentMonth, currentDate);
          });
        })(year, month, date);
        date++;
        rowHasDates = true;
      }
    }

    if (rowHasDates) {
      usedRows++; // ์‹ค์ œ ๋‚ ์งœ๊ฐ€ ์žˆ๋Š” ํ–‰๋งŒ ์ฆ๊ฐ€
    } else {
      calendarBody.removeChild(row); // ๋ถˆํ•„์š”ํ•œ ํ–‰ ์ œ๊ฑฐ
    }
  }

  container.appendChild(table);
  updateHeader(year, month);
}

function displaySelectedDate(year, month, date) {
  const displayArea = document.getElementById('selectedDateTime');
  const monthNames = ["1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"];
  const formattedDate = `${year}๋…„ ${month}์›” ${date}์ผ`;
  displayArea.textContent = formattedDate;
}

function updateHeader(year, month) {
  const monthNames = ["1์›”", "2์›”", "3์›”", "4์›”", "5์›”", "6์›”", "7์›”", "8์›”", "9์›”", "10์›”", "11์›”", "12์›”"];
  document.getElementById('currentYearMonth').textContent = `${year}๋…„ ${monthNames[month - 1]}`;
}

function changeMonth(delta) {
  currentMonth += delta;
  if (currentMonth < 1) {
    currentYear -= 1;
    currentMonth = 12;
  } else if (currentMonth > 12) {
    currentYear += 1;
    currentMonth = 1;
  }
  createCalendar(currentYear, currentMonth);
}