2

I have a Javascript code for a countdown timer on a date. I want to use this code in a online shop order process. However I don't want to edit the date every day manually. I want that the timer counts everyday to the same target time.

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  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 the result in the element with id="demo"
  document.getElementById("demo").innerHTML = "Bestellen Sie innerhalb " + hours + "Stunden und " +
    minutes + "Minuten " + "und wir versenden noch am gleichen Tag!";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
<div id="timer">
  <!-- Display the countdown timer in an element -->
  <p id="demo"></p>
</div>

Have anyone a solution for that?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Dierig Patrick
  • 168
  • 2
  • 20

2 Answers2

1
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

    // Get today's date and time
    var now = new Date().getTime();

    // Find the distance between now and the target date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    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);

    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML = days + "d " + hours + "h "
    + minutes + "m " + seconds + "s ";

    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
</script>

This is the result:

289d 4h 41m 8s

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
0

The below snippet adds 1 hour to current time and uses that as the countDownDate:

// Set the date we're counting down to
var countDownDate = new Date();
countDownDate.setHours(countDownDate.getHours() + 1);
countDownDate = countDownDate.getTime()

// Update the count down every 1 second
var x = setInterval(function() {
  // Get todays date and time
  var now = Date.now();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  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 the result in the element with id="demo"
  document.getElementById("demo").innerHTML = "Bestellen Sie innerhalb " + hours + "Stunden und " +
    minutes + "Minuten " + "und wir versenden noch am gleichen Tag!";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
<div id="timer">
  <!-- Display the countdown timer in an element -->
  <p id="demo"></p>
</div>
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • Thank your for that solution. But i need it to 2pm. Just from now to 2pm. For Example: When its 1.43pm there must be show 17 Minutes left or something. When 2pm is passed i want that the there comes the text "expired", like now. – Dierig Patrick Mar 22 '17 at 10:59