Javascript- Business hours
Blog Home | Fife Visits Blog | Fife Rant Blog | Random Computing Stuff Index

Javascript _ business hours (open-closed times) function

I wanted a script to show in text whether a business was open or closed. The file is "open.js"

//by old.graham:
//20/7/2020
//another rewrite of garage open closed times
//much shorter than the original
window.onload = openTime;

//javascript function to display open/closed in the open hours page
function openTime() {
var d = new Date(); //get the time/date
var dy = d.getDay(); //what day is it
var moment = String(Date()).slice(16,21); //time as a string
var moment = moment.replace(":",""); //remove the colon
 
if (dy == 0 || dy == 6){//saturday or sunday: closed
 
document.getElementById("sign").innerHTML = "Closed Today" ;
}else{
if(moment > 830 && moment < 1700){//is the current time between 830am and 5pm
document.getElementById("sign").innerHTML = "We're open";
}else{
document.getElementById("sign").innerHTML = "We're closed";
}
}
}
It goes with this html page, open_times.html:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<script type="text/javascript" src="open.js"></script>
		
		<title>Opening Times</title>
</head>
<body>

	<h2>Is It Open? </h2>
	<p id="sign"> </p>
</body>
</html>