How can I convert a decimal hour value like 1.6578 to hh:mm:ss in jquery or javascript?
I only managed to do it to hh:mm using this code:
var decimal
You could do something like this:
var decimalTimeString = "1.6578";
var decimalTime = parseFloat(decimalTimeString);
decimalTime = decimalTime * 60 * 60;
var hours = Math.floor((decimalTime / (60 * 60)));
decimalTime = decimalTime - (hours * 60 * 60);
var minutes = Math.floor((decimalTime / 60));
decimalTime = decimalTime - (minutes * 60);
var seconds = Math.round(decimalTime);
if(hours < 10)
{
hours = "0" + hours;
}
if(minutes < 10)
{
minutes = "0" + minutes;
}
if(seconds < 10)
{
seconds = "0" + seconds;
}
alert("" + hours + ":" + minutes + ":" + seconds);
Rather than doing the calculations yourself, use built-in functionality to set the seconds on an arbitrary date of a Date object, convert it to a string, and chop off the date part, leaving just the hh:mm:ss string.
var decimalTimeString = "1.6578";
var n = new Date(0,0);
n.setSeconds(+decimalTimeString * 60 * 60);
document.write(n.toTimeString().slice(0, 8));