Skip to content

Commit

Permalink
Merge pull request #839 from Rohit12012007/patch-11
Browse files Browse the repository at this point in the history
Update script.js
  • Loading branch information
Ayushparikh-code authored Oct 27, 2024
2 parents af3a4eb + fd238b8 commit cf9860a
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions Count-Down-To-New-Year/script.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
const dayEl = document.getElementById("days")
const dayEl = document.getElementById("days");
const hourEl = document.getElementById("hours");
const minuteEl = document.getElementById("minutes");
const secondEl = document.getElementById("seconds");
const d = 1
const m = "jan"
var y = 2024

function countdown() {
const newYear = d + m + y;
const newYearDate = new Date(newYear);
// Get the current date
const currentDate = new Date();

// Calculate the next New Year's date (Jan 1 of the next year)
const nextYear = currentDate.getFullYear() + 1;
const newYearDate = new Date(`January 1, ${nextYear} 00:00:00`);

// Get the total number of seconds between now and New Year's
const totalSeconds = (newYearDate - currentDate) / 1000;

// Convert total seconds into days, hours, minutes, and seconds
const days = Math.floor(totalSeconds / 3600 / 24);
const hours = Math.floor(totalSeconds / 3600) % 24;
const minutes = Math.floor(totalSeconds / 60) % 60;
const seconds = Math.floor(totalSeconds) % 60;

dayEl.innerHTML = formateTime(days);
hourEl.innerHTML = formateTime(hours);
minuteEl.innerHTML = formateTime(minutes);
secondEl.innerHTML = formateTime(seconds);

if(days === 0 && hours === 0 && minutes === 0 && seconds === 0){
y+=1;
}

const hours = Math.floor((totalSeconds / 3600) % 24);
const minutes = Math.floor((totalSeconds / 60) % 60);
const seconds = Math.floor(totalSeconds % 60);

// Display the countdown
dayEl.innerHTML = formatTime(days);
hourEl.innerHTML = formatTime(hours);
minuteEl.innerHTML = formatTime(minutes);
secondEl.innerHTML = formatTime(seconds);
}
function formateTime(time){
return time < 10 ? `0${time}` : time;}


// Helper function to format time (adds a leading zero if less than 10)
function formatTime(time) {
return time < 10 ? `0${time}` : time;
}

// Run the countdown immediately and update it every second
countdown();
setInterval(countdown, 1000);
setInterval(countdown, 1000);

0 comments on commit cf9860a

Please sign in to comment.