-->

的jQuery - 更改1-24小时至1-12使用小时.getHours()方法?(jQuery

2019-08-02 11:07发布

小提琴: http://jsfiddle.net/bnsex/1/

我想用2小时时钟此代码...

$(document).ready(function() {

setInterval( function() {
var hours = new Date().getHours();
$(".hours, .hour").html(( hours < 10 ? "0" : "" ) + hours);
}, 1000);

}); ​

你的帮助是非常赞赏:d

Answer 1:

您可以使用模数达到此%运营商,找到一个除法运算的余数。

例如, 11 % 1223 % 12都等于11,如一个12小时时钟将描绘。

var hours = new Date().getHours() % 12;
if (hours == 0) {
    hours += 12;
}

http://jsfiddle.net/ph7Vf/



Answer 2:

试试这个:

hours = (hours < 12 ? hours : hours - 12).toString().replace(/^\d{1}$/, '0$&');

编辑:这可能是更短的基础上,火神的回答:

hours = (hours % 12).toString().replace(/^\d{1}$/, '0$&');


文章来源: jQuery - Change 1-24 hour to 1-12 hour using .getHours() method?
标签: jquery time hour