Skip to content

Commit

Permalink
Update countdown.js
Browse files Browse the repository at this point in the history
  • Loading branch information
NotFenixio authored Apr 18, 2024
1 parent e38ef25 commit bf4fdbd
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions countdown/countdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ let Cdays = document.getElementById('days');
let Chours = document.getElementById('hours');
let Cminutes = document.getElementById('minutes');
let Cseconds = document.getElementById('seconds');

// Global variable to control weekends removal
var removeWeekends = false;

var x = setInterval(function() {

// get today's time
Expand All @@ -12,18 +16,33 @@ var x = setInterval(function() {
// finding distance
var distance = date - now;

// exclude weekends if enabled
var daysLeft = Math.floor(distance / (1000 * 60 * 60 * 24));
if (removeWeekends) {
var totalDays = Math.floor(distance / (1000 * 60 * 60 * 24));
var weekends = 0;
for (var i = 0; i <= totalDays; i++) {
var currentDate = new Date(now + (i * 24 * 60 * 60 * 1000));
var dayOfWeek = currentDate.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) { // Sunday (0) and Saturday (6)
weekends++;
}
}
daysLeft -= weekends;
}

// maths for the cells
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// display result in all the cells
Cdays.style.setProperty("--value", days);
Chours.style.setProperty("--value", hours);
Cminutes.style.setProperty("--value", minutes);
Cseconds.style.setProperty("--value", seconds);
// If finished, clear the interval so it doesn't show negative values
Cdays.style.setProperty("--value", daysLeft);
Chours.style.setProperty("--value", hours);
Cminutes.style.setProperty("--value", minutes);
Cseconds.style.setProperty("--value", seconds);

// if finished, clear the interval so it doesn't show negative values
if (distance < 0) {
clearInterval(x);
}
Expand Down

0 comments on commit bf4fdbd

Please sign in to comment.