1
+ <!DOCTYPE html>
2
+ < html lang ="en " dir ="ltr ">
3
+
4
+ < head >
5
+ < meta charset ="utf-8 ">
6
+ < title > Dynamic Calendar JavaScript | CodingNepal</ title >
7
+ < link rel ="stylesheet " href ="../stylesheet/calendar.css ">
8
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
9
+ <!-- Google Font Link for Icons -->
10
+ < link rel ="stylesheet "
11
+ href ="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200 ">
12
+ </ head >
13
+
14
+ < body >
15
+ < div class ="wrapper ">
16
+ < header >
17
+ < p class ="current-date "> </ p >
18
+ < div class ="icons ">
19
+ < span id ="prev " class ="material-symbols-rounded "> chevron_left</ span >
20
+ < span id ="next " class ="material-symbols-rounded "> chevron_right</ span >
21
+ </ div >
22
+ </ header >
23
+ < div class ="calendar ">
24
+ < ul class ="weeks ">
25
+ < li > Sun</ li >
26
+ < li > Mon</ li >
27
+ < li > Tue</ li >
28
+ < li > Wed</ li >
29
+ < li > Thu</ li >
30
+ < li > Fri</ li >
31
+ < li > Sat</ li >
32
+ </ ul >
33
+ < ul class ="days "> </ ul >
34
+ </ div >
35
+ </ div >
36
+ < script >
37
+ const daysTag = document . querySelector ( ".days" ) ,
38
+ currentDate = document . querySelector ( ".current-date" ) ,
39
+ prevNextIcon = document . querySelectorAll ( ".icons span" ) ;
40
+
41
+ // getting new date, current year and month
42
+ let date = new Date ( ) ,
43
+ currYear = date . getFullYear ( ) ,
44
+ currMonth = date . getMonth ( ) ;
45
+
46
+ // storing full name of all months in array
47
+ const months = [ "January" , "February" , "March" , "April" , "May" , "June" , "July" ,
48
+ "August" , "September" , "October" , "November" , "December" ] ;
49
+
50
+ const renderCalendar = ( ) => {
51
+ let firstDayofMonth = new Date ( currYear , currMonth , 1 ) . getDay ( ) , // getting first day of month
52
+ lastDateofMonth = new Date ( currYear , currMonth + 1 , 0 ) . getDate ( ) , // getting last date of month
53
+ lastDayofMonth = new Date ( currYear , currMonth , lastDateofMonth ) . getDay ( ) , // getting last day of month
54
+ lastDateofLastMonth = new Date ( currYear , currMonth , 0 ) . getDate ( ) ; // getting last date of previous month
55
+ let liTag = "" ;
56
+
57
+ for ( let i = firstDayofMonth ; i > 0 ; i -- ) { // creating li of previous month last days
58
+ liTag += `<li class="inactive">${ lastDateofLastMonth - i + 1 } </li>` ;
59
+ }
60
+
61
+ for ( let i = 1 ; i <= lastDateofMonth ; i ++ ) { // creating li of all days of current month
62
+ // adding active class to li if the current day, month, and year matched
63
+ let isToday = i === date . getDate ( ) && currMonth === new Date ( ) . getMonth ( )
64
+ && currYear === new Date ( ) . getFullYear ( ) ? "active" : "" ;
65
+ liTag += `<li class="${ isToday } ">${ i } </li>` ;
66
+ }
67
+
68
+ for ( let i = lastDayofMonth ; i < 6 ; i ++ ) { // creating li of next month first days
69
+ liTag += `<li class="inactive">${ i - lastDayofMonth + 1 } </li>`
70
+ }
71
+ currentDate . innerText = `${ months [ currMonth ] } ${ currYear } ` ; // passing current mon and yr as currentDate text
72
+ daysTag . innerHTML = liTag ;
73
+ }
74
+ renderCalendar ( ) ;
75
+
76
+ prevNextIcon . forEach ( icon => { // getting prev and next icons
77
+ icon . addEventListener ( "click" , ( ) => { // adding click event on both icons
78
+ // if clicked icon is previous icon then decrement current month by 1 else increment it by 1
79
+ currMonth = icon . id === "prev" ? currMonth - 1 : currMonth + 1 ;
80
+
81
+ if ( currMonth < 0 || currMonth > 11 ) { // if current month is less than 0 or greater than 11
82
+ // creating a new date of current year & month and pass it as date value
83
+ date = new Date ( currYear , currMonth , new Date ( ) . getDate ( ) ) ;
84
+ currYear = date . getFullYear ( ) ; // updating current year with new date year
85
+ currMonth = date . getMonth ( ) ; // updating current month with new date month
86
+ } else {
87
+ date = new Date ( ) ; // pass the current date as date value
88
+ }
89
+ renderCalendar ( ) ; // calling renderCalendar function
90
+ } ) ;
91
+ } ) ;
92
+ </ script >
93
+
94
+ </ body >
95
+
96
+ </ html >
0 commit comments