var seconds = 9999;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(seconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);
var seconds = 9999; // Some arbitrary value
var date = new Date(seconds * 1000); // multiply by 1000 because Date() requires miliseconds
var timeStr = date.toTimeString().split(' ')[0];
function humanizeDuration(input, units ) {
// units is a string with possible values of y, M, w, d, h, m, s, ms
var duration = moment().startOf('day').add(units, input),
format = "";
if(duration.hour() > 0){ format += "H [hours] "; }
if(duration.minute() > 0){ format += "m [minutes] "; }
format += " s [seconds]";
return duration.format(format);
}
/**
* Format a duration in seconds to a human readable format using the notion
* "h+:mm:ss", e.g. "4:40:78". Negative durations are preceeded by "-".
*
* @param t Duration in seconds
* @return The formatted duration string
*/
var readableDuration = (function() {
// Each unit is an object with a suffix s and divisor d
var units = [
{s: '', d: 1}, // Seconds
{s: ':', d: 60}, // Minutes
{s: ':', d: 60}, // Hours
];
// Closure function
return function(t) {
t = parseInt(t); // In order to use modulus
var trunc, n = Math.abs(t), i, out = []; // out: list of strings to concat
for (i = 0; i < units.length; i++) {
n = Math.floor(n / units[i].d); // Total number of this unit
// Truncate e.g. 26h to 2h using modulus with next unit divisor
if (i+1 < units.length) // Tweak substr with two digits
trunc = ('00'+ n % units[i+1].d).substr(-2, 2); // …if not final unit
else
trunc = n;
out.unshift(''+ trunc + units[i].s); // Output
}
(t < 0) ? out.unshift('-') : null; // Handle negative durations
return out.join('');
};
})();
function sec2hms(timect){
if(timect=== undefined||timect==0||timect === null){return ''};
//timect is seconds, NOT milliseconds
var se=timect % 60; //the remainder after div by 60
timect = Math.floor(timect/60);
var mi=timect % 60; //the remainder after div by 60
timect = Math.floor(timect/60);
var hr = timect % 24; //the remainder after div by 24
var dy = Math.floor(timect/24);
return padify (se, mi, hr, dy);
}
function padify (se, mi, hr, dy){
hr = hr<10?"0"+hr:hr;
mi = mi<10?"0"+mi:mi;
se = se<10?"0"+se:se;
dy = dy>0?dy+"d ":"";
return dy+hr+":"+mi+":"+se;
}
function formattime(numberofseconds){
var zero = '0', hours, minutes, seconds, time;
time = new Date(0, 0, 0, 0, 0, numberofseconds, 0);
hh = time.getHours();
mm = time.getMinutes();
ss = time.getSeconds()
// Pad zero values to 00
hh = (zero+hh).slice(-2);
mm = (zero+mm).slice(-2);
ss = (zero+ss).slice(-2);
time = hh + ':' + mm + ':' + ss;
return time;
}
var t = 34236; // your seconds
var time = ('0'+Math.floor(t/3600) % 24).slice(-2)+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2)
//would output: 09:30:36
var date = new Date(0);
date.setSeconds(45); // specify value for SECONDS here
var timeString = date.toISOString().substring(11, 19);
console.log(timeString)
// To have leading zero digits in strings.
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
// ms to time/duration
msToDuration = function(ms){
var seconds = ms / 1000;
var hh = Math.floor(seconds / 3600),
mm = Math.floor(seconds / 60) % 60,
ss = Math.floor(seconds) % 60,
mss = ms % 1000;
return pad(hh,2)+':'+pad(mm,2)+':'+pad(ss,2)+'.'+pad(mss,3);
}
它将327577转换为00:05:27.577。
更新
另一种不同场景的方法:
toHHMMSS = function (n) {
var sep = ':',
n = parseFloat(n),
sss = parseInt((n % 1)*1000),
hh = parseInt(n / 3600);
n %= 3600;
var mm = parseInt(n / 60),
ss = parseInt(n % 60);
return pad(hh,2)+sep+pad(mm,2)+sep+pad(ss,2)+'.'+pad(sss,3);
function pad(num, size) {
var str = num + "";
while (str.length < size) str = "0" + str;
return str;
}
}
toHHMMSS(6315.077) // Return 01:45:15.077
function sec2str(t){
var d = Math.floor(t/86400),
h = ('0'+Math.floor(t/3600) % 24).slice(-2),
m = ('0'+Math.floor(t/60)%60).slice(-2),
s = ('0' + t % 60).slice(-2);
return (d>0?d+'d ':'')+(h>0?h+':':'')+(m>0?m+':':'')+(t>60?s:s+'s');
}
function formatTime(seconds) {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.round(seconds % 60);
return [
h,
m > 9 ? m : (h ? '0' + m : m || '0'),
s > 9 ? s : '0' + s
].filter(Boolean).join(':');
}
//secondsToTime();
var t = wachttijd_sec; // your seconds
var hour = Math.floor(t/3600);
if(hour < 10){
hour = '0'+hour;
}
var time = hour+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2);
//would output: 00:00:00 > +100:00:00
function secondsToTime(secs)
{
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
if(hours >= 12)
{
var m= 'pm' ;
}
else
{
var m='am'
}
if(hours-12 >0)
{
var hrs = hours-12;
}
else if(hours-12 <0)
{
var hrs = hours;
}
var obj = {
"h": hrs,
"m": minutes,
"s": seconds,
"a":m
};
return obj;
}
var d = new Date();
var n = d.getHours();
var hms = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds(); // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
console.log(seconds);
console.log(secondsToTime(seconds))
/**
* Formats seconds (number) to H:i:s format.
* 00:12:00
*
* When "short" option is set to true, will return:
* 0:50
* 2:00
* 12:00
* 1:00:24
* 10:00:00
*/
export default function formatTimeHIS (seconds, { short = false } = {}) {
const pad = num => num < 10 ? `0${num}` : num
const H = pad(Math.floor(seconds / 3600))
const i = pad(Math.floor(seconds % 3600 / 60))
const s = pad(seconds % 60)
if (short) {
let result = ''
if (H > 0) result += `${+H}:`
result += `${H > 0 ? i : +i}:${s}`
return result
} else {
return `${H}:${i}:${s}`
}
}
//your date
var someDate = new Date("Wed Jun 26 2019 09:38:02 GMT+0100")
var result = `${String(someDate.getHours()).padStart(2,"0")}:${String(someDate.getMinutes()).padStart(2,"0")}:${String(someDate.getSeconds()).padStart(2,"0")}`
//result will be "09:38:02"
function durationToDDHHMMSSMS(durms) {
if (!durms) return "??";
var HHMMSSMS = new Date(durms).toISOString().substr(11, 12);
if (!HHMMSSMS) return "??";
var HHMMSS = HHMMSSMS.split(".")[0];
if (!HHMMSS) return "??";
var MS = parseInt(HHMMSSMS.split(".")[1],10);
var split = HHMMSS.split(":");
var SS = parseInt(split[2],10);
var MM = parseInt(split[1],10);
var HH = parseInt(split[0],10);
var DD = Math.floor(durms/(1000*60*60*24));
var string = "";
if (DD) string += ` ${DD}d`;
if (HH) string += ` ${HH}h`;
if (MM) string += ` ${MM}m`;
if (SS) string += ` ${SS}s`;
if (MS) string += ` ${MS}ms`;
return string;
},