将军营时间转换为正常时间

时间:2017-04-09 02:22:58

标签: javascript

我希望我的小时部分设置为1-12而不是0-23。谢谢。

以下是JavaScript:

   setInterval(function(){ time();}, 1000)

function time(){
 var dates = new Date();

 var newDates = dates.toDateString();
 // var clock = dates.toLocaleTimeString();
 var seconds = dates.getSeconds();
 var minutes = dates.getMinutes();
 var hours = dates.getHours();
 var stringSeconds= String(seconds);
 var stringMinutes= String(minutes);
 var stringHours= String(hours);
 newDate.textContent = newDates;
 newDivSeconds.textContent =  stringSeconds;
 newDivMinutes.textContent = stringMinutes + '   :' ;
 newDivHours.textContent = stringHours + '   :';

2 个答案:

答案 0 :(得分:2)

var usHours = (date.getHours() % 12) || 12;

答案 1 :(得分:1)

使用modulus运算符

var usHours = date.getHours() % 12;