В настоящее время мой код сообщает время и может смещаться на часы в масштабе 24 часа, моя проблема заключается в том, что он не позволяет мне изменять код со смещением + 30 минут, что я хотел бы сделать. Мои операторы if не дают мне желаемого результата, поэтому я начал переделывать свой код, используя modulo, чтобы помочь, но не могу найти, чтобы сделать мой html и новый код, использующий modulo для координации. Спасибо.
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(':')
}