Javascript if time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Javascript if time

Is there a way to use this code with regular time formats and not military time??? function myFunction() { var time = new Date().getHours(); var greeting; if (time < 13 ) { greeting = "David is currently in the lab writting code!"; } else { greeting = "David is not in the lab programming right now, feel free to leave a comment!!!"; } document.getElementById("status").innerHTML = greeting; } I kinda want it to be like in between the times of 7am to 11pm I'm coding....

8th Jun 2018, 3:49 PM
David CHAMPION
David CHAMPION - avatar
2 Answers
+ 2
"military time" is the reqular time format for me. Why don't you just change your if statement to if(time > 7 && time < 13) { //I'm coding } You could use this following example to use 24 hours as 12 hours system var time = new Date().getHours(); if(time > 12) { //It's pm time = (time - 12) + "pm"; } else { //It's am time = time + "am"; }
8th Jun 2018, 4:14 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
thank you
8th Jun 2018, 5:11 PM
David CHAMPION
David CHAMPION - avatar