Check for working hours - javascript

I am trying to create a boolean function to check whether the current date/hour is a working hours. Now knowing that working hours are from 9AM to 5PM weekly except Fridays & Saturdays, I am facing issues with what I have come up with. I think my code works well for checking for days, but I just can't get it to work with hours as well. Here is my code:
var dayOfWeek = now.getDay();
//weekday 0==Sunday 1==Monday 2==Tuesday 3==Wednesday 4==Thurs 5==Friday 6==Sat
//Not Friday or Saturday
if ((dayOfWeek != 5) && (dayOfWeek != 6)){
if (now.getHours() >= 9 && now.getHours() < 17 ) {
//Within working hours now."
}
else
{
//After working hours."
}
}
}
Here is my HTML Code test on JSFiddle:
http://jsfiddle.net/jp9BW/
Changing my PC clock works. My test case is a working day starting 5PM. And that's when the problem happens. The else block is not hit.

I believe, this should be enough
function isWorkingHour(now) {
return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17;
}

I would do it like this
function checkOpeningTimes() {
let date = new Date(); // current time
let hours = date.getHours();
let day = date.getDay();
let openingDays = [ 0, 1, 2, 3, 4 ];
return openingDays.includes( day ) && hours >= 9 && hours <= 17;
}

Related

JavaScript - How to handle the Daylight Savings in Europe

I am new to javascript and I am trying to console.log some values based on the business hours and off business hours in Europe
My Business hours are as below:
Monday-Thursday (08:00 - 16:00) & Friday 08:00-12:30
My Off-Business hours are as below:
Everything else apart from the business hours
Everything looks good but I am having a hard time handling the Day Light Savings.
According to the laws, the last Sunday in March and October are the breakpoints where the Daylight savings on and off.
Here's my code snippet:
function businessOrOffBusinessHours() {
var date = new Date();
var day = date.getDay();
var hours = date.getUTCHours();
var minutes = date.getMinutes();
console.log(date.getTimezoneOffset())
if (date.getTimezoneOffset() === 60) {
// Daylight savings
hours += 1;
console.log("daylight savings...")
}
hours += 2;
console.log(hours+":"+minutes)
if ((day >= 1 && day <= 4 && hours >= 8 && hours < 16) || (day === 5 && hours >= 8 && (hours <= 12 && minutes < 30))) {
console.log("Business Hours");
} else {
console.log("Off Business Hours");
}
}
businessOrOffBusinessHours()
The problem with above code is that even if I initialise the Date() object with a value like Date("2023-06-10T00:00:00Z") where the date falls exactly within the Daylight Savings window (i.e. last Sunday in March and October), I would still get the getTimezoneOffset() equals to 0
Can someone help me with this?
PS: I don't want to use any other third party javascript libraries or frameworks, I appreciate the solution to be fully in vanilla

How do you check if it is monday and after 9 o'clock javascript

I am trying to have an alert run only when it is a certain day AND it is after 9am but i cant work out how to do it, here is the code i already have:
var today = new Date().getHours();
var date = new Date();
if (today >= 9) {
if(today.getDay() == 3) {
alert('Alert 1');
}
Thanks
First, create an instance of the built-in class Date, then extract the current day and hours:
const now = new Date();
var day = now.getDay();
var hours = now.getHours();
Now you can check if it's Monday and after 9am:
if (day == 1 && hours >= 9) {
console.log("Yes");
} else {
console.log("No");
}
Notes:
The day is an integer ranging from 0 to 6, 0 being Sunday and 6 being Saturday.
The hours is a integer ranging from 0 to 23.

setting a date and time within a banner to say we are open or closed

I am trying to input a "We are open" text in the long banner on my website.
And "We are closed" between the other hours. That would mainly be 12-6pm every day apart from saturday which is 10:00-6pm.
How should I go about this?
Get time date now(current time) assign it to a variable then use if statements to compare the value to your conditions once that is done change the inner HTML of the banner depending on the if statement.
This is the php script you need, then for the UI side you should take care, not having specified in your question what are you using or any piece of code. This script checks the day of the week and the time:
$now = date("H");
$day = date("w");
if(($day != 6 && $now >= 12 && $now < 18)
|| ($day == 6 && $now >= 10 && $now < 18))
{
$status = "open";
}
else
{
$status = "closed";
}
You may then want to work with the UTC date values to avoid issues if people access your website outside the timezone where your country is.
js script which checks multiple days and office hours.
var d = new Date();
if (d.getDay() !== 7) {
if (d.getDay() !== 6) {
if (d.getHours() >= 12 && d.getHours() <= 18) {
// office hours during the week
} else {
// closed during the week
}
} else {
if (d.getHours() >= 10 && d.getHours() <= 18) {
// office hours on saturday
} else {
// closed on saturday
}
}
} else {
// code for when its sunday.
}
Remove the if (d.getDay() !==7){} if the office hours on Sunday are the same as week days.

Javascript Date/Time Check Error

using the following code in my application to display html pages depending on it being todays date and also which time of the day it is e.g morning, afternoon or evening. Currently it is 2:53pm and the code is only displaying the am html page (which is the first one). I tried to run the console.log command but got nothing in the console which could be because of wikitude.
The first function of getting the date is working correctly it is just not checking the time correctly.
var inputDate = new Date("5/17/2018");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
var hour = new Date().getHours();
console.log("hour is: " + hour);
// between 12 PM and 7 AM respectively
if(hour => 7 && hour < 12) {
//morning (Always running code here no matter what time of day)
}
else if(hour >= 12 && hour <= 18) {
//afternoon
}
else {
//evening or before 7
}
}
else{
//not today (works if date is not today)
}
You have a typo in the if statement: => should be >=
var inputDate = new Date("5/17/2018");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if (inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
var hour = new Date().getHours();
console.log("hour is: " + hour);
// between 12 PM and 7 AM respectively
if (hour >= 7 && hour < 12) {
//morning (Always displaying code here)
alert('morning')
}
else if (hour >= 12 && hour <= 18) {
//afternoon
alert('afternoon')
}
else {
//evening or before 7
alert('evening')
}
}
else {
//not today
alert('not today')
}

Using JavaScript to change a message depending on the day of the week?

I am adding functionality to a website to change the message depending on if a food truck is open. I was successfully able to make the message change depending on the time, but I'm having trouble implementing getDay() to show the closed message all day on Saturday and Sunday.
Here is the working script that I have so far:
<script language="JavaScript">
var mess1="";
var outmess= "Kendo's Cuisine "
document.write("<center><font size=+1><i><b>")
day = new Date( )
hr = day.getHours( )
if (( hr >= 0 ) && (hr <= 11 ))
mess1= "is closed right now. He's open Mon - Fri 11am - 2pm. "
if (( hr >= 11 ) && (hr < 13))
mess1=" is open right now! Call in your order to have it ready by the time you get here!"
if (( hr >= 13) && (hr <= 14))
mess1= "usually runs out of food by now! Call before you come!"
if (( hr >= 14 ) && (hr <= 24 ))
mess1= "is closed right now. He's open Mon - Fri 11am - 2pm. "
document.write("<blink>")
document.write(outmess)
document.write("</blink>")
document.write(mess1)
document.write("</b></i></font></center>")
</script>
It seems you want to put up a "closed" message outside the hours of 11:00 to 14:00 Monday to Friday, so perhaps:
function isOpen(date) {
var day = date.getDay();
var hour = date.getHours();
if (day == 0 || day == 6 || hour < 11 || hour > 13) {
// shop is closed
return false;
}
// Otherwise, the shop is open
return true;
}
Note however that if the date object is created on the client, it will be local to that timezone, which may not match wherever the shop is. So you probably need to do this based on UTC time, which will be consistent everywhere.
Use getDay() method to get the weekday from date object. It returns a number from 0 - 6 to indicate days from sunday - saturday.
So you have to check like
var day = new Date();
if(day.getDay() == 0 || day.getDay() == 6) {
alert("shop is closed");
}

Categories

Resources