简体   繁体   English

函数逻辑对javascript时钟应用没有意义

[英]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.

相关问题 这里的逻辑在 javascript 中没有意义 - the logic here doesn't make sense in javascript 基本的循环逻辑对我来说没有意义 - Basic For Loop Logic Doesn't make sense to me Fizzbuzz function 逻辑不工作:Output 顺序不正确,逻辑没有意义 - Fizzbuzz function logic not working: Output order is incorrect and logic doesn't make sense 尝试使用 Javascript 在 html 上制作时钟,但我的脚本不起作用。 时钟实际上并没有运行 - Trying to make a clock on html with Javascript, but my script isn't working. the clock doesn't actually run 如果大于 12 JavaScript 行没有任何意义,则更新小时值 - Update hours value if greater than 12 JavaScript line doesn't make any sense 试着在我的javascript代码中理解“this”(一件事有效,另一件没有) - Trying to make sense of “this” in my javascript code (one thing works, the other doesn't) jQuery 的 noConflict() 代码没有意义 - jQuery's noConflict() code doesn't make sense 进行数学运算是否有原因* 1000/1000没有意义 - is there a reason for math.round *1000 /1000 doesn't make sense ForIn 循环有效但返回没有意义 - ForIn Loop Works but return doesn't make sense 在前端 javascript 应用程序中使用 DI 容器是否有意义 - Does it make sense to use DI container in front end javascript app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM