0

我有一个代表时间的字符串。例如,让我们使用这个字符串:

var s = '06:59'

如何将时间四舍五入到最接近的小时?

4

3 回答 3

1

尝试以下操作:

var s = '06:59';
var n = s.split(':');
n[0] = Math.round( parseInt(n[0]) + parseInt(n[1])/60 ) % 24;
n[1] = '00';
var output = n.join(':');

或者更简洁地按照N20084753的建议,

var s = '06:59';
var n = s.split(':');
var output = Math.round( parseInt(n[0]) + parseInt(n[1])/60 ) % 24 + ':00';
于 2013-10-26T13:00:33.677 回答
0
var mins = parseInt(s.split(":")[1]);
var hours = parseInt(s.split(":")[0]);
var result = "";
if(mins > 30)
   hours++;
if(hours < 10)
  result = "0" + hours + ":00";
else if(hours > 23)
  result = "00:00";
else
  result = "" + hours + ":00";
于 2013-10-26T12:30:10.930 回答
0
var roundTime = function(time) { 
    var splitted = time.split(':');
    var mins  = parseInt(splitted[1]),
        hours = parseInt(splitted[0]);
    return (mins > 31 ? hours + 1 : hours ) + ":00";
 }
于 2013-10-26T13:37:08.040 回答