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

前端 未结 2 1194
暖寄归人
暖寄归人 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:15

    Try with this:

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

    Edit: It could be shorter, based on Vulcan's answer:

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

提交回复
热议问题