我想创建文本时钟,通过选中或取消选中复选框,可以使用PM / AM将格式从24小时更改为12小时。你能帮我创建这段代码吗?
以下是我现在所拥有的:
#'
function GetClock() {
var d = new Date();
var nhour = d.getHours(),
nmin = d.getMinutes();
if (nmin <= 9) nmin = "0" + nmin
document.getElementById('clockbox').innerHTML = "" + nhour + ":" + nmin + "";
}
window.onload = function() {
GetClock();
setInterval(GetClock, 1000);
}
@import url('https://fonts.googleapis.com/css?family=Roboto+Condensed:700');
#clockbox {
display: inline-block;
width: auto;
color: black;
position: fixed;
top: 38%;
left: 38%;
font-size: 10vw;
font-family: 'Roboto Condensed', sans-serif;
}
答案 0 :(得分:1)
var clockFormat = 24;
function GetClock(){
var d=new Date();
var nhour=d.getHours(),nmin=d.getMinutes();
if(nmin<=9) nmin="0"+nmin
if (clockFormat == 12 && nhour > 12){
nhour -=12;
nmin+= " PM";
}else {
if (clockFormat == 12 ){
nmin+= " AM";
}
}
document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+"";
}
window.onload=function(){
GetClock();
setInterval(GetClock,1000);
}
function setFormat(format){
clockFormat = format;
}
&#13;
@import url('https://fonts.googleapis.com/css?family=Roboto+Condensed:700');
#clockbox{
display: inline-block;
width: auto;
color:#fff;
position:fixed;
top:38%;
left:38%;
font-size: 10vw;
font-family:'Roboto Condensed', sans-serif;
}
body{
color:#fff;
background-color:black;
}
&#13;
<div id="clockbox"></div>
<input type="radio" name="format" value="24" onChange="setFormat(this.value)" checked="checked"> Format 24<br>
<input type="radio" name="format" value="12" onChange="setFormat(this.value)">Format 12<br>
&#13;