Sie können die folgende Funktion verwenden, um die Zeit (in Sekunden) in das Format HH:MM:SS
umzuwandeln :
var convertTime = function (input, separator) {
var pad = function(input) {return input < 10 ? "0" + input : input;};
return [
pad(Math.floor(input / 3600)),
pad(Math.floor(input % 3600 / 60)),
pad(Math.floor(input % 60)),
].join(typeof separator !== 'undefined' ? separator : ':' );
}
Ohne die Angabe eines Trennzeichens wird :
als (Standard-)Trennzeichen verwendet :
time = convertTime(13551.9941351); // --> AUSGABE = 03:45:51
Wenn Sie -
als Trennzeichen verwenden möchten, geben Sie es einfach als zweiten Parameter an:
time = convertTime(1126.5135155, '-'); // --> AUSGABE = 00-18-46
Demo
var convertTime = function (input, separator) {
var pad = function(input) {return input < 10 ? "0" + input : input;};
return [
pad(Math.floor(input / 3600)),
pad(Math.floor(input % 3600 / 60)),
pad(Math.floor(input % 60)),
].join(typeof separator !== 'undefined' ? separator : ':' );
}
document.body.innerHTML = '' + JSON.stringify({
5.3515555 : convertTime(5.3515555),
126.2344452 : convertTime(126.2344452, '-'),
1156.1535548 : convertTime(1156.1535548, '.'),
9178.1351559 : convertTime(9178.1351559, ':'),
13555.3515135 : convertTime(13555.3515135, ',')
}, null, '\t') + '';
Siehe auch dieses Fiddle.
15 Stimmen
Benchmarks einiger der vorgeschlagenen Antworten in diesem Thread. jsperf.com/ms-to-hh-mm-ss-time-format
0 Stimmen
Mögliche Duplikat von Sekunden in HH-MM-SS mit JavaScript umwandeln?