1- let today = new Date ( ) ;
1+ const today = new Date ( ) ;
22let currentMonth = today . getMonth ( ) ;
33let currentYear = today . getFullYear ( ) ;
4- let selectYear = document . getElementById ( "year" ) ;
5- let selectMonth = document . getElementById ( "month" ) ;
64
7- let months = [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" ] ;
5+ const selectYear = document . getElementById ( "year" ) ;
6+ const selectMonth = document . getElementById ( "month" ) ;
7+ const monthAndYear = document . getElementById ( "monthAndYear" ) ;
8+ const calendarBody = document . getElementById ( "calendar-body" ) ;
89
9- let monthAndYear = document . getElementById ( "monthAndYear" ) ;
10- showCalendar ( currentMonth , currentYear ) ;
10+ const months = [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" ,
11+ "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" ] ;
1112
13+ document . addEventListener ( "DOMContentLoaded" , ( ) => {
14+ showCalendar ( currentMonth , currentYear ) ;
15+ } ) ;
1216
1317function next ( ) {
14- currentYear = ( currentMonth === 11 ) ? currentYear + 1 : currentYear ;
15- currentMonth = ( currentMonth + 1 ) % 12 ;
16- showCalendar ( currentMonth , currentYear ) ;
18+ currentMonth = ( currentMonth + 1 ) % 12 ;
19+ if ( currentMonth === 0 ) currentYear ++ ;
20+ showCalendar ( currentMonth , currentYear ) ;
1721}
1822
1923function previous ( ) {
20- currentYear = ( currentMonth === 0 ) ? currentYear - 1 : currentYear ;
21- currentMonth = ( currentMonth === 0 ) ? 11 : currentMonth - 1 ;
22- showCalendar ( currentMonth , currentYear ) ;
24+ currentMonth = ( currentMonth === 0 ) ? 11 : currentMonth - 1 ;
25+ if ( currentMonth === 11 ) currentYear -- ;
26+ showCalendar ( currentMonth , currentYear ) ;
2327}
2428
2529function jump ( ) {
26- currentYear = parseInt ( selectYear . value ) ;
27- currentMonth = parseInt ( selectMonth . value ) ;
30+ const selectedYear = parseInt ( selectYear . value ) ;
31+ const selectedMonth = parseInt ( selectMonth . value ) ;
32+
33+ if ( ! isNaN ( selectedYear ) && ! isNaN ( selectedMonth ) ) {
34+ currentYear = selectedYear ;
35+ currentMonth = selectedMonth ;
2836 showCalendar ( currentMonth , currentYear ) ;
37+ }
2938}
3039
3140function showCalendar ( month , year ) {
32-
33- let firstDay = ( new Date ( year , month ) ) . getDay ( ) ;
34- let daysInMonth = 32 - new Date ( year , month , 32 ) . getDate ( ) ;
35-
36- let tbl = document . getElementById ( "calendar-body" ) ; // body of the calendar
37-
38- // clearing all previous cells
39- tbl . innerHTML = "" ;
40-
41- // filing data about month and in the page via DOM.
42- monthAndYear . innerHTML = months [ month ] + " " + year ;
43- selectYear . value = year ;
44- selectMonth . value = month ;
45-
46- // creating all cells
47- let date = 1 ;
48- for ( let i = 0 ; i < 6 ; i ++ ) {
49- // creates a table row
50- let row = document . createElement ( "tr" ) ;
51-
52- //creating individual cells, filing them up with data.
53- for ( let j = 0 ; j < 7 ; j ++ ) {
54- if ( i === 0 && j < firstDay ) {
55- let cell = document . createElement ( "td" ) ;
56- let cellText = document . createTextNode ( "" ) ;
57- cell . appendChild ( cellText ) ;
58- row . appendChild ( cell ) ;
59- }
60- else if ( date > daysInMonth ) {
61- break ;
62- }
63-
64- else {
65- let cell = document . createElement ( "td" ) ;
66- let cellText = document . createTextNode ( date ) ;
67- if ( date === today . getDate ( ) && year === today . getFullYear ( ) && month === today . getMonth ( ) ) {
68- cell . classList . add ( "bg-info" ) ;
69- } // color today's date
70- cell . appendChild ( cellText ) ;
71- row . appendChild ( cell ) ;
72- date ++ ;
73- }
74-
75-
41+ const firstDay = new Date ( year , month ) . getDay ( ) ;
42+ const daysInMonth = 32 - new Date ( year , month , 32 ) . getDate ( ) ;
43+
44+ // Clear previous calendar
45+ calendarBody . innerHTML = "" ;
46+
47+ // Set month and year
48+ monthAndYear . textContent = `${ months [ month ] } ${ year } ` ;
49+ selectYear . value = year ;
50+ selectMonth . value = month ;
51+
52+ let date = 1 ;
53+
54+ for ( let i = 0 ; i < 6 ; i ++ ) {
55+ const row = document . createElement ( "tr" ) ;
56+
57+ for ( let j = 0 ; j < 7 ; j ++ ) {
58+ const cell = document . createElement ( "td" ) ;
59+
60+ if ( i === 0 && j < firstDay ) {
61+ cell . textContent = "" ;
62+ } else if ( date > daysInMonth ) {
63+ break ;
64+ } else {
65+ cell . textContent = date ;
66+
67+ // Highlight today
68+ if (
69+ date === today . getDate ( ) &&
70+ year === today . getFullYear ( ) &&
71+ month === today . getMonth ( )
72+ ) {
73+ cell . classList . add ( "bg-info" ) ;
7674 }
7775
78- tbl . appendChild ( row ) ; // appending each row into calendar body.
76+ date ++ ;
77+ }
78+
79+ row . appendChild ( cell ) ;
7980 }
8081
81- }
82+ calendarBody . appendChild ( row ) ;
83+ }
84+ }
0 commit comments