目前,我的代码告诉时间并且可以在 24 小时范围内偏移数小时,我的问题是它不允许我更改我希望能够执行的 + 30 分钟偏移量的代码。我的 if 语句没有得到我想要的结果,所以我开始使用模数重新设计我的代码以提供帮助,但无法找到使我的 html 和使用模数协调的新代码。谢谢。
HTML 代码:
function startTime() {
var d = new Date();
var h = d.getUTCHours();
var m = d.getUTCMinutes();
var s = d.getUTCSeconds();
h = checkHour(h);
m = checkMinute(m);
s = checkTime(s);
document.getElementById('txt3').innerHTML = "" + checkHour(h - 5) + ":" + checkMinute(m + 30) + ":" + s;
document.getElementById('txt1').innerHTML = +checkHour(h + 2) + ":" + m + "";
document.getElementById('txt2').innerHTML = +h + ":" + m + "";
document.getElementById('txt4').innerHTML = "" + checkHour(h + 8) + ":" + m + "";
document.getElementById('txt5').innerHTML = "0" + checkHour(h - 10) + ":" + m + "";
}
function checkTime(i) {
var j = i;
if (i < 10) {
j = "0" + i;
}
return j;
}
function checkHour(i) {
var j = i;
if (i > 23) {
j = j - 24;
}
if (i < 0) {
j = "0" + i;
}
return j;
}
function checkMinute(i) {
var j = i;
if (i > 59) {
j = j + 30;
}
if (i < 0) {
j = "0" + i;
}
return j;
}
setInterval(function() {
startTime();
}, 500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<font color="white" face="Montserrat">
<span style="font-size:110px; border: 4px solid crimson; position:absolute; left:25px; top:440px;" id="txt1"></span>
</font>
<font color="white" face="Montserrat">
<span style="font-size:110px; border: 4px solid crimson; position: absolute; left:475px; top:440px;" id="txt3"></span>
</font>
<font color="white" face="Montserrat">
<span style="font-size:110px; border: 4px solid crimson; position: absolute; left:225px; top:540px;" id="txt2"></span>
</font>
<font color="white" face="Montserrat">
<span style="font-size:110px; border: 4px solid crimson; position:absolute; left:870px; top:540px;" id="txt4"></span>
</font>
<font color="white" face="Montserrat">
<span style="font-size:110px; border: 4px solid crimson; position:absolute; left:1075px; top:440px" id="txt5"></span>
</font>
使用模数的 Javascript,我可以用它代替我当前的时间代码:
/* var date = new Date(null);
date.setSeconds(timeInSeconds);
// retrieve time ignoring the browser timezone - returns hh:mm:ss
var utc = date.toUTCString();
// negative start index in substr does not work in IE 8 and earlier
var time = utc.substr(utc.indexOf(':') - 2, 8)
// retrieve each value individually - returns h:m:s
var time = date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds();
// does not work in IE8 and below - returns hh:mm:ss
var time = date.toISOString().substr(11, 8);
// not recommended - only if seconds number includes timezone difference
var time = date.toTimeString().substr(0, 8); */
console.log(display(60 * 60 * 2.5 + 25)) // 2.5 hours + 25 seconds
function display (seconds) {
const format = val => `0${Math.floor(val)}`.slice(-2)
const hours = seconds / 3600
const minutes = (seconds % 3600) / 60
return [hours, minutes, seconds % 60].map(format).join(':')
}