我的问题是: 如何将分钟转换为时间(hh:mm:ss)?
我有一个值:
decimalMinuteString =" 1.1783471074380165"
预期输出为:" 00:01:10"
如何使用javascript / jquery?
由于
答案 0 :(得分:2)
powtac 在另一个问题中给出了一个非常巧妙的答案,其中需要将秒转换为相同的格式。
更改他提供的解决方案以适合您的问题,以下原型函数可用于将分钟转换为HH:MM:SS字符串格式。
String.prototype.minsToHHMMSS = function () {
var mins_num = parseFloat(this, 10); // don't forget the second param
var hours = Math.floor(mins_num / 60);
var minutes = Math.floor((mins_num - ((hours * 3600)) / 60));
var seconds = Math.floor((mins_num * 60) - (hours * 3600) - (minutes * 60));
// Appends 0 when unit is less than 10
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
// Use it as following:
myDecimalNumber.minsToHHMMSS();
请参阅以下代码段中的工作代码:
String.prototype.minsToHHMMSS = function () {
var mins_num = parseFloat(this, 10); // don't forget the second param
var hours = Math.floor(mins_num / 60);
var minutes = Math.floor((mins_num - ((hours * 3600)) / 60));
var seconds = Math.floor((mins_num * 60) - (hours * 3600) - (minutes * 60));
// Appends 0 when unit is less than 10
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
var decimalMinuteString = '1.1783471074380165';
var timeString = decimalMinuteString.minsToHHMMSS();
var input = document.getElementById('input');
var output = document.getElementById('output');
input.innerText = decimalMinuteString;
output.innerText = timeString;
&#13;
<p>
Input: <span id="input"></span>
</p>
<p>
Output: <span id="output"></span>
</p>
&#13;
如果此解决方案对您有所帮助,请首先提出powtac's answer,因为它是答案的基础。
答案 1 :(得分:0)
我需要将X分钟转换为HH:MM(小时:分钟),我使用以下代码来完成此操作:
MINUTES = X; //some integer
var m = MINUTES / 60;
var h = (MINUTES-m)/60;
var HHMM = h.toString() + ":" + (m<10?"0":"") + m.toString();