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

问题描述:

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

我想使用12小时时钟作为此代码...

I want to use 12 hour clock for this code...

$(document).ready(function() {

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

}); ​

非常感谢您的帮助:D

Your help is much appreciated :D

您可以使用模数%运算符来实现此目的,该运算符可以找到除法运算的其余部分.

You can achieve this using the modulus % operator, which finds the remainder of a division operation.

例如11 % 1223 % 12都等于11,就像12小时制一样.

For example, 11 % 12 and 23 % 12 both equal 11, like a 12-hour clock would portray.

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

http://jsfiddle.net/ph7Vf/