G
G
grenline1231232021-06-30 19:33:47
JavaScript
grenline123123, 2021-06-30 19:33:47

Timer output correction?

Now it displays the time in this format
3:50:39
03:50:39 or 05:05:05 is required, that is, if the number is not decimal, it should be before the number 0

var now = Date.now();

      var distance = date - now;

      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);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-06-30
@grenline123123

For inspiration:

for (let i = 8; i < 13; i++) console.log( ('00' + i).substr(-2));
/* выведет:
08
09
10
11
12
*/

For education:
for (let i = 8; i < 13; i++) console.log( i.toString().padStart(2, '0'));
// тот же результат
String.padStart()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question