David Kamnatnik David Kamnatnik - 8 hours ago 2
HTML Question

JavaScript Countdown Timer change text when reaches 1 day, 1 second, 1 minute

I have a JavaScript count down timer with plural labels in the HTML: "Days", "Hours", "Minutes", "Seconds". I need to change the label in the HTML to the singular version when the each respective timer reaches "1", so "Day", "Hour" etc...

How would I go about doing this? Thank you in advance.



var eventDate = 'Dec 16 2017 9:24:18 GMT-0400';

function time_remaining(endtime) {
var t = Date.parse(endtime) - Date.parse(new Date());
var seconds = Math.floor((t / 1000) % 60);
var minutes = Math.floor((t / 1000 / 60) % 60);
var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
var days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}

function run_clock(id, endtime) {
var clock = document.getElementById(id);

// get spans where our clock numbers are held
var days_span = clock.querySelector('.days');
var hours_span = clock.querySelector('.hours');
var minutes_span = clock.querySelector('.minutes');
var seconds_span = clock.querySelector('.seconds');

function update_clock() {
var t = time_remaining(endtime);

// update the numbers in each part of the clock
days_span.innerHTML = t.days;
hours_span.innerHTML = ('0' + t.hours).slice(-2);
minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
seconds_span.innerHTML = ('0' + t.seconds).slice(-2);

if (t.total <= 0) {
clearInterval(timeinterval);
document.getElementById("clockdiv").style.display = "none";
}

}
update_clock();
var timeinterval = setInterval(update_clock, 1000);

}
run_clock('clockdiv', eventDate);

.clockWrap {
text-align: center;
font-weight: 700;
font-size: 50px;
max-width: 100%;
}

h1.clock {
color: #595959;
font-size: 30px;
line-height: 36px;
margin: 0;
padding: 50px 0 30px;
}

.day {
padding-right: 44px;
}

.colon {
font-size: 30px;
padding-right: 25px;
padding-left: 25px;
line-height: 60px;
}


/*--Colors--*/

.red {
color: #fc5852;
}

.blue {
color: #24a5f4;
}

.orange {
color: #ffaa00;
}

.grey {
color: #b3b3b3;
}

.green {
color: #35b134;
}

#clockdiv {
display: inline-block;
text-align: center;
}

#clockdiv>div {
padding-bottom: 50px;
display: inline-block;
}

.smalltext {
padding-top: 5px;
font-size: 18px;
line-height: 24px;
font-weight: 400;
text-transform: uppercase;
}

@media only screen and (max-width: 540px) {
.timeWrapper {
width: 167px;
margin: auto;
padding-bottom: 15px;
}
h1.clock {
font-size: 20px;
line-height: 36px;
padding: 18px 0 5px;
}
.day {
padding-right: 0;
}
.colon {
display: none !important;
}
#clockdiv {
text-align: left !important;
}
#clockdiv>div {
padding-bottom: 4px;
display: inline-block;
}
.smalltext {
padding-top: 19px;
padding-left: 15px;
font-size: 18px;
line-height: 24px;
float: right;
}
}

<div class="clockWrap">
<div class="timeWrapper">
<div id="clockdiv">
<h1 class="clock">Event will start in</h1>
<div class="day blue"> <span class="days"></span>
<div class="smalltext">Days</div>
</div>
<div class="orange"><span class="hours"></span>
<div class="smalltext">Hours</div>
</div>
<div class="colon grey"><span>:</span>
<div class="smalltext">&nbsp;</div>
</div>
<div class="green"><span class="minutes"></span>
<div class="smalltext">Minutes</div>
</div>
<div class="colon grey"> <span>:</span>
<div class="smalltext">&nbsp;</div>
</div>
<div class="red"><span class="seconds"></span>
<div class="smalltext">Seconds</div>
</div>
</div>
</div>
</div>




Answer

You could do something like this:

var eventDate = 'Jul 18 2017 20:24:18 GMT-0400';

function time_remaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function run_clock(id, endtime) {
  var clock = document.getElementById(id);

  // get spans where our clock numbers are held
  var days_span = clock.querySelector('.days');
  var hours_span = clock.querySelector('.hours');
  var minutes_span = clock.querySelector('.minutes');
  var seconds_span = clock.querySelector('.seconds');

  var days_title = document.getElementById('days_title');
  var hours_title = document.getElementById('hours_title');
  var mins_title = document.getElementById('mins_title');
  var secs_title = document.getElementById('secs_title');

  function update_clock() {
    var t = time_remaining(endtime);

    // update the numbers in each part of the clock
    days_span.innerHTML = t.days;
    // Determine title for Day(s)
    if (t.days === 1) {
      days_title.innerHTML = 'Day';
    } else {
      days_title.innerHTML = 'Days';
    }
    // Determine title for Hour(s)
    hours_span.innerHTML = ('0' + t.hours).slice(-2);
    if (t.hours === 1) {
      hours_title.innerHTML = 'Hour';
    } else {
      hours_title.innerHTML = 'Hours';
    }
    // Determine title for Min(s)
    minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
    if (t.minutes === 1) {
      mins_title.innerHTML = 'Minute';
    } else {
      mins_title.innerHTML = 'Minutes';
    }
    // Determine title for Sec(s)
    seconds_span.innerHTML = ('0' + t.seconds).slice(-2);
    if (t.seconds === 1) {
      secs_title.innerHTML = 'Second';
    } else {
      secs_title.innerHTML = 'Seconds';
    }

    if (t.total <= 0) {
      clearInterval(timeinterval);
      document.getElementById("clockdiv").style.display = "none";
    }

  }
  update_clock();
  var timeinterval = setInterval(update_clock, 1000);

}
run_clock('clockdiv', eventDate);
.clockWrap {
  text-align: center;
  font-weight: 700;
  font-size: 50px;
  max-width: 100%;
}

h1.clock {
  color: #595959;
  font-size: 30px;
  line-height: 36px;
  margin: 0;
  padding: 50px 0 30px;
}

.day {
  padding-right: 44px;
}

.colon {
  font-size: 30px;
  padding-right: 25px;
  padding-left: 25px;
  line-height: 60px;
}


/*--Colors--*/

.red {
  color: #fc5852;
}

.blue {
  color: #24a5f4;
}

.orange {
  color: #ffaa00;
}

.grey {
  color: #b3b3b3;
}

.green {
  color: #35b134;
}

#clockdiv {
  display: inline-block;
  text-align: center;
}

#clockdiv>div {
  padding-bottom: 50px;
  display: inline-block;
}

.smalltext {
  padding-top: 5px;
  font-size: 18px;
  line-height: 24px;
  font-weight: 400;
  text-transform: uppercase;
}

@media only screen and (max-width: 540px) {
  .timeWrapper {
    width: 167px;
    margin: auto;
    padding-bottom: 15px;
  }
  h1.clock {
    font-size: 20px;
    line-height: 36px;
    padding: 18px 0 5px;
  }
  .day {
    padding-right: 0;
  }
  .colon {
    display: none !important;
  }
  #clockdiv {
    text-align: left !important;
  }
  #clockdiv>div {
    padding-bottom: 4px;
    display: inline-block;
  }
  .smalltext {
    padding-top: 19px;
    padding-left: 15px;
    font-size: 18px;
    line-height: 24px;
    float: right;
  }
}
<div class="clockWrap">
  <div class="timeWrapper">
    <div id="clockdiv">
      <h1 class="clock">Event will start in</h1>
      <div class="day blue"> <span class="days"></span>
        <div class="smalltext" id="days_title"></div>
      </div>
      <div class="orange"><span class="hours"></span>
        <div class="smalltext" id="hours_title"></div>
      </div>
      <div class="colon grey"><span>:</span>
        <div class="smalltext">&nbsp;</div>
      </div>
      <div class="green"><span class="minutes"></span>
        <div class="smalltext" id="mins_title"></div>
      </div>
      <div class="colon grey"> <span>:</span>
        <div class="smalltext">&nbsp;</div>
      </div>
      <div class="red"><span class="seconds"></span>
        <div class="smalltext" id="secs_title"></div>
      </div>
    </div>
  </div>
</div>