[英]functions logic doesn't make sense for javascript clock app
function showTime () { let date = new Date(); let hours = date.getHours(); //0-23 let minutes = date.getMinutes(); //0-59 let seconds = date.getSeconds(); //0-59 let formatHours = convertFormat(hours); hours = checkTime(hours); hours = addZero(hours); minutes = addZero(minutes); seconds = addZero(seconds); document.getElementById('clock').innerHTML = hours + ':' + minutes + ':' + seconds + formatHours; } function convertFormat(time) { let format = 'AM'; if (time >= 12) { format = 'PM'; } return format; } function checkTime(time) { if (time > 12) { time = time - 12; } if (time === 0) { time = 12; } return time; } function addZero(time) { if (time < 10) { time = "0" + time; } return time; } showTime(); setInterval(showTime, 1000);
body { min-height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; } #clock { font-family: 'Orbitron'; font-size: 5rem; color: limegreen; }
<!DOCTYPE html> <html> <head> <title>Clocck</title> <link rel="stylesheet" type="text/css" href="style.css"> <link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet"> </head> <body> <div id="clock"> </div> <script type="text/javascript" src="script.js"></script> </body> </html>
Hey guys I'm currently working on a simple clock app. 大家好,我目前正在开发一个简单的时钟应用程序。 There's a particular function that doesn't make much sense to me.
有一个特定的功能对我来说意义不大。 The convertFormat function's if statement sets up time to equal 'PM' if it is >= 12. Well the reverse is happening to me.
如果convertFormat函数的> = 12,则它的if语句将其设置为等于“ PM”的时间。 Its reading 8:33pm?
阅读时间是晚上8:33? when it checks the hours variable and reads 8 it should switch format to am?
当它检查小时变量并读取8时,应将格式切换为am? right?
对?
The reason that when you see the time is 8:33PM, it still display PM instead of AM because the date.getHours() function will return your "system clock", not the clock value display by your function. 当您看到时间是8:33 PM时,它仍然显示PM而不是AM,因为date.getHours()函数将返回“系统时钟”,而不是函数显示的时钟值。 It mean that when you mistakenly think the input variable for your convertFormat function is "8", it is actually 20 in your system clock.
这意味着当您错误地认为convertFormat函数的输入变量为“ 8”时,实际上是系统时钟中的20。
Actually, it makes sense. 实际上,这是有道理的。 The function displays the time in 12 hour format so even though its 8:33 but in the evening, it will show as 8:33 pm.
该功能以12小时格式显示时间,因此即使是8:33,但在晚上,它将显示为8:33 pm。 If you want to see 8:33 as AM only and 20:33 as PM, change the function to use 24 hr format
如果只想将8:33视为AM,将20:33视为PM,请将功能更改为使用24小时格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.