Output x if it is weekend - javascript

I have created this to check if time is between 17:00 / 23:00.
This works fine! But now I want to check if its weekend.
Can you help me?
var objDate = new Date();
var hours = objDate.getHours();
if (hours >= 17 && hours <= 23) {
document.write('<!--test1 -->');
}
else {
document.write('<!--test2 -->');
}

Use the getDay() method of the Date object (0 corresponds to Sunday and 6 to Saturday):
<script language="javascript">
var objDate = new Date();
var hours = objDate.getHours(),
dayOfWeek = objDate.getDay();
if(hours >= 17 && hours <= 23 && (dayOfWeek == 6 || dayOfWeek == 0)){
document.write('<!--test1 -->');
} else {
document.write('<!--test2 -->');
}
</script>

var objDate = new Date();
var hours = objDate.getHours();
var n = objDate.getDay();
if(hours >= 17 && hours <= 23){
alert('<!--test1 -->');
}
else{
alert('<!--test2 -->');
}
if(n == 0 || n == 6)
{
alert('Weekend');
}
else
{
alert('Weekday');
}

Related

Javascript time condition between 11:40 AM and 12:20PM (11:40 < 12:20)

I tried the code below but it didn't work.
var hour = new Date().getHours();
var min = new Date().getMinutes();
if (hour >= 11 && hour <= 12 && min < 40 && min > 20) {
document.body.style.background = "green";
} else {
document.body.style.background = "red";
}
you had three problems here:
the extra } at the end
hours is not defined. I changed it to hour, which is defined
'=>': this is not necessary for the hours. since you are only checking two hours (11 and 12), it's much simpler to check each hour and it's minuets separately.
var hour = new Date().getHours();
var min = new Date().getMinutes();
if((hour == 11 && min >40) || hour == 12 && min < 20) {
document.body.style.background = "red";
} else {
document.body.style.background = "green";
}
This is just for this example.
var hour = new Date().getHours();
var min = new Date().getMinutes();
if (hour >= 11 && hour <= 12 && (hour === 11 ? min > 40 : min < 20)) {
document.body.style.background = "red";
} else {
document.body.style.background = "green";
}
You can use the javascript Date object and compare with the Date object between start time, end time and current time.
const date1 = new Date();
date1.setHours(11);
date1.setMinutes(40);
const date2 = new Date();
date2.setHours(12);
date2.setMinutes(20);
const today = new Date();
if (today >= date1 && today <= date2) {
document.body.style.background = "red";
} else {
document.body.style.background = "green";
}
Your logic had some problems so I fixed them. This will change background to red if between the hours of 11:40 AM and 12:20 PM (inclusive):
var hour = (new Date()).getHours();
var min = (new Date()).getMinutes();
if (hour == 11 && min >= 40 || hour == 12 && min <= 20) {
document.body.style.background = "red";
} else {
document.body.style.background = "green";
}

Conditional statement not functioning properly

Depending on the time of the day. I am trying to display Morning, Afternoon or Evening to the user using the JavaScript ( new Date() ) method. But what I am receiving in response is just the (else) statement. Can I get advice as to what I am doing incorrectly?
const mainHeader = document.getElementById("header");
var greetings = () => {
var today = new Date();
}
window.onload= (e) => {
greetings;
if(today >= 1 && today < 12) {
mainHeader.innerHTML = "<h3>Good Morning! And Welcome To Guess The Number</h3>"
console.log("Morning: between 1 and 12");
} else if(today >= 12 && today < 17) {
mainHeader.innerHTML = "<h3>Good Afternoon! And Welcome To Guess The Number</h3>"
console.log("Afternoon: between 12 and 17");
} else if(today >= 17 && today <= 24) {
mainHeader.innerHTML = "<h3>Good Evening! And Welcome To Guess The Number</h3>"
console.log("Evening: between 17 and 24");
} else {
mainHeader.innerHTML = "<h3>Welcome to Guess The Number</h3>"
console.log("No time zone");
}
}
You need to return the hour from greetings(), and then assign it to a variable in the window.onload function.
const mainHeader = document.getElementById("header");
var greetings = () => {
var today = new Date();
return today.getHours();
}
window.onload = (e) => {
var today = greetings()
if (today >= 1 && today < 12) {
mainHeader.innerHTML = "<h3>Good Morning! And Welcome To Guess The Number</h3>"
console.log("Morning: between 1 and 12");
} else if (today >= 12 && today < 17) {
mainHeader.innerHTML = "<h3>Good Afternoon! And Welcome To Guess The Number</h3>"
console.log("Afternoon: between 12 and 17");
} else if (today >= 17 && today <= 24) {
mainHeader.innerHTML = "<h3>Good Evening! And Welcome To Guess The Number</h3>"
console.log("Evening: between 17 and 24");
} else {
mainHeader.innerHTML = "<h3>Welcome to Guess The Number</h3>"
console.log("No time zone");
}
}
<div id="header">
</div>
You never define today in the onload function(even if you called greeting, today wouldn't be defined b/c var is function scoped). Instead replace greetings; with var today = new Date().getHours(); and remove the greetings function.
If you still want to keep greetings as a function, you can replace it with var greetings = ()=>new Date().getHours(); and replace greetings; with var today= greeting();

Javascript check if the time is between 8:30am and 6:30pm

Ok so what the title says. I am doing this on a server, so I get the server's time using some PHP code. The problem is that it is a time frame without exact round hour values. Should I use nested if else statements?
var serverTimestampMillis = <?php print time() * 1000 ?>;
var checkInterval = 1000;
var checkTime = function () {
serverTimestampMillis += checkInterval;
var now = new Date(serverTimestampMillis);
var timeDiv = document.getElementById('timeDiv');
var messageDiv = document.getElementById('messageDiv');
timeDiv.innerHTML = now.toString();
var dayOfWeek = now.getDay(); // 0 = Sunday, 1 = Monday, ... 6 = Saturday
var hour = now.getHours(); // 0 = 12am, 1 = 1am, ... 18 = 6pm
var minutes = now.getMinutes();
// check if it's Monday to Thursday between 8:30am and 6:30pm
// this is where I don't know how to check 8:30
if (dayOfWeek > 0 && dayOfWeek < 5 && hour > 8 && hour < 18) {
messageDiv.innerHTML = 'Yes, we are open!';
messageDiv.className='open';
}
else {
messageDiv.innerHTML = 'Sorry, we\'re closed!';
messageDiv.className='closed';
}
};
// check the time every 1000 milliseconds
setInterval(checkTime, checkInterval);
checkTime();
thank you in advance, and sorry for being a noob
Compare between two dates using a helper function:
function createDateTime(time) {
var splitted = time.split(':');
if (splitted.length != 2) return undefined;
var date = new Date();
date.setHours(parseInt(splitted[0], 10));
date.setMinutes(parseInt(splitted[1], 10));
date.setSeconds(0);
return date;
}
var startDate = createDateTime("8:30");
var endDate = createDateTime("17:30");
var now = new Date();
var isBetween = startDate <= now && now <= endDate;
console.log(isBetween);
JSFIDDLE.
You can just nest your statements, like you said (to make it easier to read), and then check the specific edge cases (8:30-9 and 18:00-18:30).
if (dayOfWeek > 0 && dayOfWeek < 5) {
if ((hour > 8 && hour < 18) ||
(hour == 8 && minutes >= 30) ||
(hour == 18 && minutes <= 30)) {
messageDiv.innerHTML = 'Yes, we are open!';
messageDiv.className='open';
}
}

Get week days and specific time of day

I'm using javaScript to display an element on week days and in busines hours (Monday to Friday between 08:00 and 16:50).
I'm using this code:
var date = new Date();
var thisMin = date.getMinutes();
var thisHour = date.getHours();
var thisWeekday = date.getDay();
// True if Mon - Fri between 08:00 - 16:50
if (thisWeekday > 0 && thisWeekday < 6 && thisHour > 7 && (thisHour < 17 && thisMin <= 50)) {
alert("True");
} else {
alert("False");
}
- but is this the best way to code it? It seems like a lot of conditions and it's working all the time... Can it be done in a better way with javaScript?
Fiddle here.
Thanks.
Note that you have a condition with two distinct parts "must be within week interval" and "must be within time interval". The code can reflect this more clearly by being explicit.
For the time comparison I would reshape the current time so that you can use Date's <= and >= operators instead of reimplementing time comparisons yourself:
var startTime = new Date(0, 0, 0, 8, 0);
var endTime = new Date(0, 0, 0, 16, 50);
var startWeekday = 1;
var endWeekday = 5;
function shouldDisplay() {
var d = new Date();
function withinWeekInterval() {
return (d.getDay() >= startWeekday &&
d.getDay() <= endWeekday);
}
function withinTimeInterval() {
var now = new Date(0, 0, 0, d.getHours(), d.getMinutes());
return (now >= startTime &&
now <= endTime);
}
return (withinWeekInterval() && withinTimeInterval());
}
Manual test:
alert(shouldDisplay());
Try below Code :
var date = new Date();
var thisMin = date.getMinutes();
var thisHour = date.getHours();
var thisWeekday = date.getDay();
var isSuccess = false;
// True if Mon - Fri between 08:00 - 16:50
if (thisWeekday > 0 && thisWeekday < 6 && thisHour > 7 && (thisHour < 17)) {
if (thisHour == 16 && thisMin > 50) {
isSuccess = false;
} else {
isSuccess = true;
}
} else {
isSuccess = false;
}
alert(isSuccess);

Load css by date with javascript

I'm looking to load different css files according to the date (season).
I tried modifying an image script from here on stackoverflow, but that didn't work.
Can anybody point me out where it goes wrong?
<link href="/css_season/default.css" rel="stylesheet" type="text/css" onload="logo(this)">
function logo(link) {
var d = new Date();
var Today = d.getDate();
var Month = d.getMonth();
var src;
if (Month === 4 && (Today >= 1 && Today <= 30)) {
src = "/css_season/easter.css";
} else if (Month === 7 && (Today >= 1 && Today <= 31)) {
src = "/css_season/vacation.css";
} else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2)) {
src = "/css_season/vacation.css";
} else if (Month === 12 && (Today >= 15 && Today <= 31)) {
src = "/css_season/holidays.css";
}
link.href=href;
}
Your assignment link.href=href won't work because href isn't defined. Also, I would put the logo function in <body onload="logo();"> and give your link tag an id attribute.
This should work for you:
<html>
<head>
<link id="cssId" href="/css_season/default.css" rel="stylesheet" type="text/css"/>
</head>
<body onload="logo();">
<!-- body content here -->
</body>
function logo() {
var d = new Date();
var Today = d.getDate();
var Month = d.getMonth();
var src;
if (Month === 4 && (Today >= 1 && Today <= 30))
src = "/css_season/easter.css";
else if (Month === 7 && (Today >= 1 && Today <= 31))
src = "/css_season/vacation.css";
else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2))
src = "/css_season/vacation.css";
else if (Month === 12 && (Today >= 15 && Today <= 31))
src = "/css_season/holidays.css";
document.getElementById('cssId').href=src;
}
You have wrong assignment at the end of the js function.
link.href=href; should be link.href = src;
Cheers

Categories