Spaces:
Runtime error
Runtime error
/** | |
* λ¬λ ₯ λ λλ§ ν λ νμν μ 보 λͺ©λ‘ | |
νμ¬ μ (μ΄κΈ°κ° : νμ¬ μκ°) | |
κΈμ λ§μ§λ§μΌ λ μ§μ μμΌ | |
μ μ λ§μ§λ§μΌ λ μ§μ μμΌ | |
*/ | |
function calendarInit() { | |
// λ μ§ μ 보 κ°μ Έμ€κΈ° | |
var date = new Date(); // νμ¬ λ μ§(λ‘컬 κΈ°μ€) κ°μ Έμ€κΈ° | |
var utc = date.getTime() + (date.getTimezoneOffset() * 60 * 1000); // utc νμ€μ λμΆ | |
var kstGap = 9 * 60 * 60 * 100; // νκ΅ kst κΈ°μ€μκ° λνκΈ° | |
var today = new Date(utc + kstGap); // νκ΅ μκ°μΌλ‘ date κ°μ²΄ λ§λ€κΈ°(μ€λ) | |
console.log("Today : ", today) | |
// λ¬λ ₯μμ νκΈ°νλ λ μ§ κ°μ²΄ | |
var thisMonth = new Date(today.getFullYear(), today.getMonth(), today.getDate()); | |
var currentYear = thisMonth.getFullYear(); // λ¬λ ₯μμ νκΈ°νλ μ° | |
var currentMonth = thisMonth.getMonth(); // λ¬λ ₯μμ νκΈ°νλ μ | |
var currentDate = thisMonth.getDate(); // λ¬λ ₯μμ νκΈ°νλ μΌ | |
// kst κΈ°μ€ νμ¬μκ° | |
console.log("thisMonth"); | |
console.log(currentYear); | |
console.log(currentMonth); // monthIndex | |
console.log(currentDate); | |
console.log(thisMonth); | |
// μΊλ¦°λ λλλ§ | |
renderCalender(thisMonth); | |
//////////////////////////////////////////////////////////////// | |
function renderCalender(thisMonth, help=0) { | |
// λλλ§μ μν λ°μ΄ν° μ 리 | |
currentYear = thisMonth.getFullYear(); | |
currentMonth = thisMonth = thisMonth.getMonth(); | |
if (help != 1) { | |
// currentDate = thisMonth.getDate(); // 1 - 31 : 1 - 31 | |
currentDate = new Date(today.getFullYear(), today.getMonth(), today.getDate()).getDate(); | |
} | |
// μ΄μ λ¬μ λ§μ§λ§λ λ μ§μ μμΌ κ΅¬νκΈ° | |
var startDay = new Date(currentYear, currentMonth, 0); | |
var prevDate = startDay.getDate(); // 1 - 31 : 1 - 31 | |
var prevDay = startDay.getDay(); // Sunday - Saturday : 0 - 6 | |
// μ΄λ² λ¬μ λ§μ§λ§λ λ μ§μ μμΌ κ΅¬νκΈ° | |
var endDay = new Date(currentYear, currentMonth + 1 , 0); | |
var nextDate = endDay.getDate(); // 1 - 31 : 1 - 31 | |
var nextDay = endDay.getDay(); // Sunday - Saturday : 0 - 6 | |
// console.log(prevDate, prevDay, nextDate, nextDay, currentMonth); | |
// νμ¬ μ νκΈ° | |
$('.year-month').text(currentYear + '.' + (currentMonth + 1)); | |
// λλλ§ html μμ μμ± | |
calendar = document.querySelector('.dates') | |
calendar.innerHTML = ''; | |
// μ§λλ¬ | |
for (var i = prevDate - prevDay + 1; i <= prevDate; i++) { | |
calendar.innerHTML = calendar.innerHTML + '<div class="day prev disable">' + i + '</div>' | |
} | |
// μ΄λ²λ¬ | |
for (var i = 1; i <= nextDate; i++) { | |
calendar.innerHTML = calendar.innerHTML + '<div class="day current">' + i + '</div>' | |
} | |
// λ€μλ¬ | |
for (var i = 1; i <= (7 - nextDay == 7 ? 0 : 7 - nextDay); i++) { | |
calendar.innerHTML = calendar.innerHTML + '<div class="day next disable">' + i + '</div>' | |
} | |
// μ€λ λ μ§ νκΈ° | |
if (today.getMonth() == currentMonth) { | |
todayDate = today.getDate(); | |
var currentMonthDate = document.querySelectorAll('.dates .current'); // μ€κ° λμ΄μ°κΈ° μ£Όμ | |
// var currentMonthDate = document.querySelectorAll('div.day.current'); // κ°μ μλ―Έ | |
// console.log(currentMonthDate) | |
currentMonthDate[todayDate-1].classList.add('today'); | |
} | |
// μ΄μ λ¬λ‘ μ΄λ | |
$('.go-prev').on('click', function() { | |
if (help == 0) { | |
thisMonth = new Date(currentYear, currentMonth - 1, 1); | |
renderCalender(thisMonth, 1); | |
} | |
else { | |
renderCalender(thisMonth, 1); | |
} | |
}) | |
// λ€μλ¬λ‘ μ΄λ | |
$('.go-next').on('click', function() { | |
if (help == 0) { | |
thisMonth = new Date(currentYear, currentMonth + 1, 1); | |
renderCalender(thisMonth, 1); | |
} | |
else { | |
renderCalender(thisMonth, 1); | |
} | |
}) | |
} | |
} |