mdj1412
쀑간 μˆ˜μ •(λ―Έμ™„μ„±)
8d21d0b
/**
* 달λ ₯ λ Œλ”λ§ ν•  λ•Œ ν•„μš”ν•œ 정보 λͺ©λ‘
ν˜„μž¬ μ›” (μ΄ˆκΈ°κ°’ : ν˜„μž¬ μ‹œκ°„)
κΈˆμ›” λ§ˆμ§€λ§‰μΌ λ‚ μ§œμ™€ μš”μΌ
μ „μ›” λ§ˆμ§€λ§‰μΌ λ‚ μ§œμ™€ μš”μΌ
*/
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);
}
})
}
}