R
R
Rapen2016-11-07 14:13:19
JavaScript
Rapen, 2016-11-07 14:13:19

How to write a method to convert number of seconds to hh:mm:ss?

A function fTime that takes a non-negative value as an argument and returns the time in HH:MM:SS format.
HH-hour two-digit 00-99
mm - minutes, two-digit 00-59
ss - seconds, two-digit 00-59
Maximum accepted value fTimer("359999") // "99:59:59";
fTime("86399")= "23:59:59";
fTimer("60") // "00:00:59"
I stumbled on this task... It seems easy

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-11-07
@vshvydky

359999 / 60 / 60 = 99.99972222222222 round down - > 99
359999 / 60 - 99 * 60 = 5999.983333333333 - 5940 = 59.983333333333 round down - > 59
* 60 * 60 359999 - 356400 - 3540 = 359999 - 356400 - 3540 = 59
Naturally, we check the presence of hours and minutes through the condition <1, which is essentially not necessary, since we will equate everything that is less than 1 to 0.
For example, 90 seconds, this is 1, 5 minutes, hours 0
respectively
90 / 60 / 60 = 0.025 down is 0

D
Dmitry Eremin, 2016-11-07
@EreminD

In short, like this:

var timeA = new Date(0) //говорим, что есть timeA с минимальным значением
console.log(timeA) //Thu Jan 01 1970 03:00:00 GMT+0300 (RTZ 2 (зима))
timeA.setSeconds(99) //добавляем секунды (проверку на неотрицательность сделайте выше)
console.log(timeA) //Thu Jan 01 1970 03:01:39 GMT+0300 (RTZ 2 (зима))
//Бум! готово

JS itself will add the required number of seconds and convert to minutes/hours/days/years if necessary

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question