Mihir Kadiya
Mihir Kadiya

Reputation: 11

Countdown time between start time and end time

I am working on jQuery, I want to countdown time between start time and end time. I have start time and end time and also get difference between them but how to countdown difference variable time. my code look like this

  var receivedTime = $('.external-event').attr('data-receivedtime');
            var now = new Date();
            var difference = moment.utc(moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(receivedTime, "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss");

I Want this type of time in difference variable : 05:47:50

Upvotes: 0

Views: 3158

Answers (3)

Nofi
Nofi

Reputation: 2175

Java Script Count down timer [HH:mm:ss]

  <head>
    <script data-require="[email protected]" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div>
      <span class="hour">05</span>
 :       <span class="min">00</span>
 :       <span class="sec">00</span>
    </div>
  </body>

</html>

JS

$( document ).ready(function() {
  function changeTime(){
    var hour = parseInt($(".hour").text());
    var min = parseInt($(".min").text());
    var sec = parseInt($(".sec").text());
    if(hour==0 && min==0 && sec==0){
      clearInterval(clock);
      timer = false;
    }
    if(timer){
    if(sec==0){
       $(".sec").text(59);
      if(min==0){
      $(".min").text(59);
         $(".hour").text(--hour);
      }
      else
      $(".min").text(--min);
    }
    else{
      $(".sec").text(--sec);

    }

  }

  }
  var timer = true;
 var clock = setInterval(changeTime,1000);

});

Demo

Upvotes: 0

Nofi
Nofi

Reputation: 2175

but it works for "05:47" .Try to follow the logic and proceed...

<body>
    <div>Registration closes in <span id="time">05:00</span> minutes!</div>
</body>

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(minutes + ":" + seconds);

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

jQuery(function ($) {
    var fiveMinutes = 60 * 5,
        display = $('#time');
    startTimer(fiveMinutes, display);
});

Demo

Upvotes: 0

Er Sharad Soni
Er Sharad Soni

Reputation: 310

For more jquery countdown examples please use the below link:

http://hilios.github.io/jQuery.countdown/examples.html

Upvotes: 1

Related Questions