0

I have a jquery script that does a countdown showing days, hours, minutes, and seconds.

Now I want to remove the days from the script and it should start the countdown from 2 hours. I haven't been able to accomplish this.

Here's my code:

<p id="demo"></p>
<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 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 = days + "d " + hours + "h "
        + minutes + "m " + seconds + "s ";

        // If the count down is finished, write some text
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("demo").innerHTML = "EXPIRED";
        }
    }, 1000);
</script>
Adnan Umer
  • 3,669
  • 2
  • 18
  • 38
Martin
  • 1
  • 4
  • This does not seem to involve jQuery at all. `new Date("Jan 5, 2018 15:37:25")` would be much better as `new Date("2018, 0, 5, 15, 37, 25)`. By "*should start countdown from 2hours*" do you mean it should count down two hours from the time it's called? – RobG Apr 07 '17 at 09:22
  • Probably a duplicate of [*The simplest possible JavaScript countdown timer*](http://stackoverflow.com/questions/20618355/the-simplest-possible-javascript-countdown-timer). – RobG Apr 07 '17 at 09:29
  • @RobG. I mean it should start the countdown from 2hrs the time its called.(As i load the page it should start immediately from 2hrs till it reaches 0hrs 0 mins 0 secs. – Martin Apr 07 '17 at 09:43
  • i have gotten a solution by using the link RobG gave me thanks. but now if i want to start the timer on button postback. how can i? – Martin Apr 07 '17 at 09:49
  • —if you have another question, then ask another question. You should also either post your own answer to this question and accept it, or delete the question since you seem to not be looking for an answer any more. – RobG Apr 07 '17 at 23:57

0 Answers0