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);
                }
            })
        }
    }