There is a script that calculates the time until tomorrow. Under each line I wrote comments. I do not understand how the number of minutes is calculated: (seconds % 3600) / 60 . And number of seconds: (seconds % 3600) % 60 .

 var tomorrow = new Date().setHours(24, 0, 0); (function foo() { var now = new Date(); // Сколько секунд осталось до завтра. var seconds = parseInt(tomorrow - now) / 1000; // 3600 - кол-во секунд в часе. Сколько часов осталось до завтра var hh = parseInt( seconds / 3600 ); // var mm = parseInt( (seconds % 3600) / 60 ); var ss = parseInt( (seconds % 3600) % 60 ); document.body.innerHTML = hh + ":" + mm + ":" + ss ; setTimeout(foo, 1000); })(); 

    2 answers 2

    The% operator returns the remainder of the division. So the result

     seconds % 3600 

    this is the number of seconds of an incomplete hour

    here (seconds % 3600) / 60 they are translated into minutes

    and here (seconds % 3600) % 60 by the same principle, seconds of not a full minute are obtained.


    Example:

    Take for example seconds=4000

    In this case, we get one hour (3600) plus an incomplete hour - how many minutes are less than 60

     seconds % 3600 = 4000 % 3600 = 400 

    Check how many minutes it is.

     400 / 60 = 6.666666666666667 

    we drop the fractional part - we get 6 minutes.

    It remains to know the number of seconds

     400 % 60 = 40 

    Total: 1 full hour, 6 full minutes, 40 seconds

    • How to understand part-time? If you remove the division by 60 and leave seconds % 3600 , then at the very beginning of the hour there will be 3600, i.e. full hour - Aleksandr
    • one
      @Aleksandr, now I will try to add an example :) - Grundy
    • well thank you. - Aleksandr

    Isn't it easier to count hours, minutes and seconds separately?

    Like that:

     var now, hh, mm, ss; now = new Date(); now.setSeconds(now.getSeconds() - 1); // Корректируем время на одну секунду, иначе будет результат меньше на одну секунду hh = 23 - now.getHours(); mm = 59 - now.getMinutes(); ss = 59 - now.getSeconds(); 

    Additionally, you can add the correct time display, provided that it is 00:00:00

     if (!(hh || mm || ss)) hh = 24; 

    To display hours, minutes and seconds with leading zeros, you can do this:

     str = ('0' + hh).slice(-2) + ':' + ('0' + mm).slice(-2) + ':' + ('0' + ss).slice(-2); 

     setInterval(function() { var now, hh, mm, ss; now = new Date(); now.setSeconds(now.getSeconds() - 1); // Корректируем время на одну секунду, иначе будет результат меньше на одну секунду hh = 23 - now.getHours(); mm = 59 - now.getMinutes(); ss = 59 - now.getSeconds(); if (!(hh || mm || ss)) hh = 24; document.getElementById('countdown').innerHTML = ('0' + hh).slice(-2) + ':' + ('0' + mm).slice(-2) + ':' + ('0' + ss).slice(-2); }, 500); 
     <div id="countdown"></div> 

    • Easier, but I would like to know how it is calculated in my example. - Aleksandr
    • 2
      You have a difference in seconds. seconds % 3600 is the difference in seconds within an hour. We divide it by 60, take the whole - we get the minutes. Take the remainder of the division by 60 - we get seconds. It seems everything is transparent. - tutankhamun
    • @tutankhamun, it looks like the calculations are not quite correct, see the example for 23:00:00 - you will get: h=1 , mm=60 , ss=60 , and for 23:59:59 - h=1 , mm=1 , ss=1 - Grundy
    • @tutankhamun, yes in response, added a comment above with an example - Grundy
    • @tutankhamun, generally speaking is still not right :) check on the same examples - Grundy