- This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
Viewing 3 posts - 1 through 3 (of 3 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › If store is open?
I’ve got this code that basically says that if it’s Monday, Tuesday or Thursday and the clock is more than 09:00 and less than 15:00 then we show that it’s open, if else it’s closed.
What is wrong with my code?
var currentTime = new Date();
var day = currentTime.getDate();
var datePlace = document.getElementById("datePlace");
var hour = currentTime.getHours();
if ((day == "1" | day == "2" | day == "4") && (hours<15 && hours>9)){
datePlace.innerHTML = "Me er åpne!";
}else{
datePlace.innerHTML = "Me er stengt!";
}
After marking this as solved, I guess I could give out the code that I ended up with :)
var currentTime = new Date();
var hour = currentTime.getHours();
var day = currentTime.getDay();
var datePlace = document.getElementById("datePlace");
if ((day == "1" || day == "2" || day == "4") && (hour<15 && hour>9)){
datePlace.innerHTML = "Me er opne!";
$("#datePlace").css("text-shadow", "0px 0px 1px lightgreen");
}else{
datePlace.innerHTML = "Me er stengt!";
}
Hi schart!
It should be currentTime.getDay();
not currentTime.getDate();
to get the day of the week.
The other really big problem with that code is that it’ll work fine on your local computer, but people in a different time zone will see the time offset, so you may need to use an online clock with GMT – check out this answer on StackOverflow.
I’d also use PHP or something else server-side for this, personally.