1- const dayEl = document . getElementById ( "days" )
1+ const dayEl = document . getElementById ( "days" ) ;
22const hourEl = document . getElementById ( "hours" ) ;
33const minuteEl = document . getElementById ( "minutes" ) ;
44const secondEl = document . getElementById ( "seconds" ) ;
5- const d = 1
6- const m = "jan"
7- var y = 2024
85
96function countdown ( ) {
10- const newYear = d + m + y ;
11- const newYearDate = new Date ( newYear ) ;
7+ // Get the current date
128 const currentDate = new Date ( ) ;
9+
10+ // Calculate the next New Year's date (Jan 1 of the next year)
11+ const nextYear = currentDate . getFullYear ( ) + 1 ;
12+ const newYearDate = new Date ( `January 1, ${ nextYear } 00:00:00` ) ;
13+
14+ // Get the total number of seconds between now and New Year's
1315 const totalSeconds = ( newYearDate - currentDate ) / 1000 ;
16+
17+ // Convert total seconds into days, hours, minutes, and seconds
1418 const days = Math . floor ( totalSeconds / 3600 / 24 ) ;
15- const hours = Math . floor ( totalSeconds / 3600 ) % 24 ;
16- const minutes = Math . floor ( totalSeconds / 60 ) % 60 ;
17- const seconds = Math . floor ( totalSeconds ) % 60 ;
18-
19- dayEl . innerHTML = formateTime ( days ) ;
20- hourEl . innerHTML = formateTime ( hours ) ;
21- minuteEl . innerHTML = formateTime ( minutes ) ;
22- secondEl . innerHTML = formateTime ( seconds ) ;
23-
24- if ( days === 0 && hours === 0 && minutes === 0 && seconds === 0 ) {
25- y += 1 ;
26- }
27-
19+ const hours = Math . floor ( ( totalSeconds / 3600 ) % 24 ) ;
20+ const minutes = Math . floor ( ( totalSeconds / 60 ) % 60 ) ;
21+ const seconds = Math . floor ( totalSeconds % 60 ) ;
22+
23+ // Display the countdown
24+ dayEl . innerHTML = formatTime ( days ) ;
25+ hourEl . innerHTML = formatTime ( hours ) ;
26+ minuteEl . innerHTML = formatTime ( minutes ) ;
27+ secondEl . innerHTML = formatTime ( seconds ) ;
2828}
29- function formateTime ( time ) {
30- return time < 10 ? `0${ time } ` : time ; }
31-
29+
30+ // Helper function to format time (adds a leading zero if less than 10)
31+ function formatTime ( time ) {
32+ return time < 10 ? `0${ time } ` : time ;
33+ }
34+
35+ // Run the countdown immediately and update it every second
3236countdown ( ) ;
33- setInterval ( countdown , 1000 ) ;
37+ setInterval ( countdown , 1000 ) ;
0 commit comments