Why is this simple (nested) if statment not working? - javascript

I'm looking to achieve the following:
If it's Saturday or Sunday, show the we're closed page.
If it's not Saturday or Sunday:
.If it is outside of working hours (not between 9am and 12pm), show the we're closed page.
.If it is within working hours, show the we're open page.
Here is the code:
<html>
<head>
<TITLE>Golborne Patient Booking Portal </TITLE>
<script language="JavaScript" type="text/javascript">
function ampmRedirect() {
var currentTime = new Date();
var currentHour = currentTime.getHours();
var d = new Date();
var n = d.getDay();
if (n == 0) || (n == 6){ // if Saturday or Sunday
window.location.href = "closed.html"; // we are closed
} else { // if it is any other day
if ((currentHour < 8) || (currentHour > 11)) { // if outside of working hours (8am to noon)
window.location.href = "closed.html"; // we are closed
} else { // anything else (not weekday or within the times)
window.location.href = "open.html"; // we are open
}
}
}
</script>
</head>
<body onload="ampmRedirect()">
</body>
</html>
Where am I going wrong?

Beside the missing parenthese around the condition expression, you could take a single check with day and time and assign either a closing page or another target.
function ampmRedirect() {
var date = new Date(),
time = date.getHours(),
day = date.getDay();
window.location.href = day === 0 || day === 6 || time < 8 || time > 11
? "closed.html"
: "https://golborne.abtrace-cloud.com";
}
<body onload="ampmRedirect()">

Related

Script to open URL only on first Friday of the month and between 8am-10am

I'm working in Virtual Tour Software with Javascript and want to make an action, which will open an URL (with photo panorama) ONLY in particular part of time (in that case it's first Friday of month and only between 8am and 10am). Any ideas? Thanks Michal
For now I got somethink like this:
var startDate = new Date('Jun 5, 2020 8:00:00').getTime();
var endDate = new Date('Jun 5, 2020 10:00:00').getTime();
setInterval(function() {
var now = new Date().getTime();
var visible = now > startDate && now < endDate;
var hotspot = this.getPanoramaOverlayByName(this.getMediaByName('Panorama'), 'Hotspot');
if(hotspot && hotspot.get('enabled') != visible)
hotspot.set('enabled', visible);
}.bind(this), 1000);`
The problem is that I need to change "var start" and "var end" every time. I want to make it visible on every First Friday of month.
you can use getday() and getdate() function in js to find the day which is friday and date which is less or equal to 7 whatever the month and year is.
you can add following in your code like this:
var d = new Date();
var startDate = d.getDate(); // this will gives you only date
var isFriday = d.getDay(); // this will gives index of the day
var n = d.getHours(); // add this new line gethours
if((startDate <= 7 && isFriday == 5) && (n >= 8 && n <= 10)) // as indexing 5 for friday
{
setInterval(function() {
var hotspot = this.getPanoramaOverlayByName(this.getMediaByName('Panorama'), 'Hotspot');
if(hotspot && hotspot.get('enabled') != true)
hotspot.set('enabled', true);
}.bind(this), 1000);
}
try this.

Based on date and time, show/hide element

I want to show and hide an element on a page based on multiple dates and times. For instance, I have an alert box that I want to show for the following dates 4/28, 4/29, and 4/30 between the hours of 9 am to 12 pm PST. And hide for the remaining hours. The posted code works for an open and closing time but I am unsure how I can add multiple dates. Or I'm just missing something.
Edit: I suck at jQuery/JavaScript.
HTML
<div class="open openstatus">Open</div>
<div class="closed openstatus">Closed</div>
JS
var now = new Date(),
currentDay = now.getDay(),
openTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
13,
02
),
closeTime = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
13,
03
),
open = now.getTime() > openTime.getTime() && now.getTime() < closeTime.getTime();
if (currentDay !== 6 && currentDay !== 0 && open) {
$('.openstatus').toggle();
}
I'd do something like this:
HTML
<div id="alertBox">My alert box</div>
JAVASCRIPT
function myFunc(){
var d = new Date(); //client Date
d.setMinutes(d.getMinutes() + d.getTimezoneOffset()); //To get current GMT time
d.setHours(d.getHours() - 8); // This is time in PST
var day = d.getDay(),
month = d.getMonth(),
hours = d.getHours(),
dateOfMonth = d.getDate();
var prohibbittedMonth = 4;
var prohibbittedDates =[28, 29, 30];
var showAlertBox = false;
if(day === 0 || day === 6){
showAlertBox = true; // If the day is Saturday or Sunday
}else if(month = prohibbittedMonth && prohibbittedDates.includes(dateOfMonth) && !(hours >= 0 && hours <9)){
showAlertBox = true;
}
// You may combine the above two condition in a single line. I have just separated them for better readability.
if(!showAlertBox){
document.getElementById('alertBox').style.display = "none"; //You may use your jquery here if you like
}else{
document.getElementById('alertBox').style.display = ""; //You may use your jquery here if you like
}
}
myFunc();
Cant't you add sth like this
currentDate=now.getDate();
if (currentDay !== 6 && currentDay !== 0 && open && (currentDate==28||currentDate==29||currentDate==30))
This checks if the currentDate is either 28 or 29 or 30.

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')
}

Check for working hours

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;
}

How do I change a picture during the weekends with javascript?

I want to show on our site when the chat is available. The problem is that it won't be open in the weekends. How do I exclude them? Can it be done?
<html>
<body>
<script type="text/javascript">
function chatonoff(){
var now = new Date();
var hour = now.getHours();
if (hour >=9 && hour <=18)
{
document.getElementById("chat").src = "/bilder/butik/chat-open.png";
}
}
</script>
<img id="chat" src="/bilder/butik/chat.png" onload="chatonoff()">
</body>
</html>
You could do like this
function chatonoff(){
var now = new Date();
var hour = now.getHours();
var day = now.getDay();
//Check if weekend : in this case, I assume that saturday == 6 and Sunday = 0
//It depends on your location / timezone
if(day != 6 && day != 0)
{
if (hour >=9 && hour <=18)
{
document.getElementById("chat").src = "/bilder/butik/chat-open.png";
}
}
}
EDIT
About excluding hollidays :
You should create an array with all the off dates. Then, check if the curent day is present in the array
var offDaysListArray = ['2013-01-01','2013-01-02'];
var now = new Date();
var y = now .getFullYear();
var d = (now .getDate() < 10) ? '0'+now .getDate() : now .getDate();
var m = ((now .getMonth()+1) < 10) ? '0'+(now .getMonth()+1) : (now .getMonth()+1);
//Check if it is a closed day
if(offDaysListArray.indexOf(y + '-' + m + '-' + d) != -1)
return false; //It is a close day
Be carefull with indexOf, old browser like IE8 doesn't implement this function. Check Why doesn't indexOf work on an array IE8?
This is a very simple answer, although it won't exactly work as you want it to as JavaScript is run on the client side.
This means that someone on the other side of the world will be able to see your chat on HIS FRIDAY even though it is a SATURDAY for YOU (A Sunday/Monday example also works here). Ideally you would solve this by using some server side language such as Java / .NET / PHP or whatever you are using.
Here is your quick JavaScript fix:
<html>
<body>
<script type="text/javascript">
function chatonoff(){
var now = new Date();
var hour = now.getHours();
var day = now.getDay();
if (hour >=9 && hour <=18 && day >= 1 && day <= 5)
{
document.getElementById("chat").src = "/bilder/butik/chat-open.png";
}
}
</script>
<img id="chat" src="/bilder/butik/chat.png" onload="chatonoff()">
</body>
</html>

Categories

Resources