2016-10-21 83 views
0

我有一個小時間計算器,它可以從24小時時間內減去分鐘數。然而,從13:30等於13:08的22分鐘,而不是顯示「13:08」,我的網頁顯示「13:8」有無論如何顯示「13: 8」?全格式顯示時間計算

的Javascript:

function myFunction() { 

function converToMinutes(s) { 
    var c = s.split(':'); 
    return parseInt(c[0]) * 60 + parseInt(c[1]); 
} 

function parseTime(s) { 
    return Math.floor(parseInt(s)/60) + ":" + parseInt(s) % 60 
} 


var endTime = document.getElementById("etime").value; 
var startTime = converToMinutes(document.getElementById("stime").value); 
var converted = parseTime(startTime - endTime); 


document.getElementById('finishtime').innerHTML = converted; 

} 

HTML:

<p>Please enter minutes</p> 
<input type="text" id="etime"> 
<br> 
<p>Please enter time in 24 hour format (eg. 15:00)</p> 
<input type="text" id="stime"> 
<br> 
<br> 
<button onclick="myFunction()">Try it</button> 

<p id="finishtime"> 
+0

你要檢查分鐘低於10,所以你可以放置一個** 0 **該值的前面。 – NewToJS

+0

'('0'+(parseInt(s)%60))。slice(-2)' – Keith

回答

0

更換

return Math.floor(parseInt(s)/60) + ":" + parseInt(s) % 60; 

var seconds = parseInt(s) % 60; 
return Math.floor(parseInt(s)/60) + ":" + ((seconds < 10)?"0"+seconds:seconds); 
+0

謝謝,工作出色 – Hsan

0

function myFunction() { 
 

 
function converToMinutes(s) { 
 
    var c = s.split(':'); 
 
    return parseInt(c[0]) * 60 + parseInt(c[1]); 
 
} 
 

 
function parseTime(s) { 
 
    return Math.floor(parseInt(s)/60) + ":" + parseInt(s) % 60 
 
} 
 
    function parseTime2(s) { 
 
    return Math.floor(parseInt(s)/60) + ":0"+ parseInt(s) % 60 
 
} 
 

 

 
var endTime = document.getElementById("etime").value; 
 
    
 
var startTime = converToMinutes(document.getElementById("stime").value); 
 
    
 
    var a= (startTime - endTime)%60; 
 
    var converted 
 
    if(a>10){ 
 
converted = parseTime(startTime - endTime); 
 
    } 
 
    else{ 
 
    converted = parseTime2(startTime - endTime); 
 
    } 
 

 

 
document.getElementById('finishtime').innerHTML = converted; 
 

 
}
<p>Please enter minutes</p> 
 
<input type="text" id="etime"> 
 
<br> 
 
<p>Please enter time in 24 hour format (eg. 15:00)</p> 
 
<input type="text" id="stime"> 
 
<br> 
 
<br> 
 
<button onclick="myFunction()">Try it</button> 
 

 
<p id="finishtime">

0

function myFunction() { 
 
\t function converToMinutes(s) { 
 
\t var c = s.split(':'); 
 
\t return parseInt(c[0]) * 60 + parseInt(c[1]); 
 
\t } 
 
\t function parseTime(s) { 
 
\t var seconds = parseInt(s) % 60; 
 
\t \t return Math.floor(parseInt(s)/60) + ":" + ((seconds < 10)?"0"+seconds:seconds); 
 
\t } 
 
    \t var endTime = document.getElementById("etime").value; 
 
\t var startTime = converToMinutes(document.getElementById("stime").value); 
 
\t var converted = parseTime(startTime - endTime); 
 
\t document.getElementById('finishtime').innerHTML = converted; 
 
}
<p>Please enter minutes</p> 
 
<input type="text" id="etime" placeholder="Please enter minutes" value="22"> 
 
<br> 
 
<p>Please enter time in 24 hour format (eg. 15:00)</p> 
 
<input type="text" id="stime" placeholder="Please enter time formate (eg. 15:00)" value="13:30"> 
 
<br> 
 
<br> 
 
<button onclick="myFunction()">Try it</button> 
 

 
<p id="finishtime"></p>