本文介绍了如何将十进制小时值转换为hh:mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将十进制小时值(如1.6578)转换为hh:mm:ss in jquery或javascript?
How can I convert a decimal hour value like 1.6578 to hh:mm:ss in jquery or javascript?
我只能设法用hh:mm使用此代码:
I only managed to do it to hh:mm using this code:
var decimaltime= "1.6578";
var hrs = parseInt(Number(decimaltime));
var min = Math.round((Number(decimaltime)-hrs) * 60);
var clocktime = hrs+':'+min;
推荐答案
你可以这样做:
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);
这篇关于如何将十进制小时值转换为hh:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!