Check if weekday and time match - javascript

I'm currently looking for a solution to check if weekday is Tuesday to wednesday
so i want to display a message on my website from tuesday 10 pm to wednesday 05 am
How i can build this in a if else sniped?

If you're talking about the user's browser timezone:
// get time now
// local to user's timezone
var now = new Date();
// check if it is Tuesday and after 10 --OR-- check if it is Wednesday and before 5
if((now.getDay() == 2 && now.getHours() >= 22) || (now.getDay() == 3 && now.getHours() < 5))
{
// do yo thing chicken wing
}

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.

How to show content timezone specific using javascript & html

My website currently displays the .pastTV div during the hours of
12:00pm-12:30pm EST. However, the code is not time-zone specific, capturing the local time of anyone visiting site.
Is there a way to make this specific to a UTC offset or timezone so that everyone is seeing (or not seeing) the same element?
My code works as expected when a visitor is in the Eastern timezone, otherwise it reads the local user time.
Update: Used this code instead for the same effect
var NYDate = new Date(new Date().toLocaleString("en-US", {timeZone: "America/New_York"}));
var NYHour = NYDate.getHours();
var NYMins = NYDate.getMinutes()
//12pm
if (NYHour >= 12 && NYHour <= 12 &&
NYMins > 0 && NYMins < 30) {
$('.pasttv, .life2').toggle();
}
var Now = new Date();
var CurrentDay = Now.getDay();
// opening time - 24 hours so 12:00pm is 12, 00
var OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 12, 00);
// closing time - 24 hours so 12:30pm is 12, 30
var ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 12, 30);
var Open = (Now.getTime() > OpeningTime.getTime() && Now.getTime() < ClosingTime.getTime())
// days 0.sun 1.mon 2.tues 3.wed 4.thur 5.fri 6.sat
// CurrentDay !== 0 && the # is the day to eclude, so if I want to be closed on Sat6, Sun0, Wed3
// CurrentDay !== 6 && CurrentDay !== 0 && CurrentDay !== 3 && Open
if (CurrentDay !== 0 && CurrentDay !== 6 && Open) {
$('.pasttv').toggle();
}
.hours {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="hours pasttv">
<font class="show-title">Past TVense</font><br> The first shows of the television medium
</div>
Have you tried using this? Date.prototype.getTimezoneOffset()
More info below:
"The time-zone offset is the difference, in minutes, from local time to UTC. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, for time zone UTC+10:00 (Australian Eastern Standard Time, Vladivostok Time, Chamorro Standard Time), -600 will be returned."
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Counting weekday cycles in JavaScript

My school runs on a 7 day cycle, so if today (2016/02/26) was day 1, tomorrow would be day 0, Monday would be day 2, and the next day 1 would be 2016/03/08. I know it's very strange, but I'm trying to find a way to use the Date object in JavaScript and add on one cycle, that is 7 days, not including weekends.
I would like to emphasize that weekends DO NOT COUNT towards day counting. I'm trying to find a way to omit weekends and easily find the next day 1 or day 5 or whatever.
There are either 1 or 2 weekends in your 7-day school cycle, depending on the start day of the cycle, so the actual cycle length is either 9 or 11 days. The Date.getDay() method gives you access to the day of the week, so a possible solutions might look like this:
var myDate= new Date();
switch(true) {
//Sunday=0, Saturday=6
case(myDate.getDay() % 6 == 0) : alert('weekend!'); return;
case (myDate.getDay() < 4) : // Mon, Tues, Wed
myDate.setDate(myDate.getDate() + 9);
break;
case (myDate.getDay() < 6) : // Thu, Fri
myDate.setDate(myDate.getDate() + 11);
break;
}

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