Spaces:
Runtime error
Runtime error
File size: 4,675 Bytes
8d21d0b |
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 |
/**
* λ¬λ ₯ λ λλ§ ν λ νμν μ 보 λͺ©λ‘
νμ¬ μ (μ΄κΈ°κ° : νμ¬ μκ°)
κΈμ λ§μ§λ§μΌ λ μ§μ μμΌ
μ μ λ§μ§λ§μΌ λ μ§μ μμΌ
*/
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);
}
})
}
} |