jQuery - Change 1-24 hour to 1-12 hour using .getHours() method?

前端 未结 2 1226
暖寄归人
暖寄归人 2021-01-27 11:34

Fiddle: http://jsfiddle.net/bnsex/1/

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

$(document).ready(function() {

setInterval( function() {
var hours = n         


        
2条回答
  •  感动是毒
    2021-01-27 12:12

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

    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/

提交回复
热议问题