Show div between mon-fri 9-5 jquery - javascript

Just need a bit of help with getting a div to fire between mon-fri 9-5:
HTML
<div class="liveperson">
<!-- Start liveperson code -->
<!-- End Liveperson code -->
</div>
jQ
$(document).ready(function() {
var rightNow = getHours();
var day = rightNow.getUTCDay();
if (day == 1-5 || rightNow == 9-13) {
$('.liveperson').show();
}
});
Can't tell what it is I'm doing wrong!
Any help would be muchly appreciated!
Thanks in advance people!

Try this:
var d = new Date();
var dayOfWeek = d.getDay();
var hour = d.getHours();
if (dayOfWeek > 0 && dayOfWeek < 6 && hour > 8 && hour < 17) {
$('.liveperson').hide();
}
dayOfWeek is 0-6, Sunday (0) through Saturday (6).
hour is is 0-23 (0 being 12 am, 23 being 11 pm).
jsFiddle example

// get current time
var d = new Date(),
hours = d.getHours(),
mins = d.getMinutes();
day = d.getDay();
// if day is mon-Fri and time is between 9am and 5:30pm
if(0 < dday < 6
&& hours >= 9
&& (hours < 17 || hours === 17 && mins <= 30)){
$('.liveperson').show();
};
This should do it.

Related

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.

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.

How to add in "working times" if statement a specify holiday dates

I try to figure out how in to the code "if statement" add a specific day(official holiday dates) in month when business does not work. Now it's code works if day is Mon, Tue, Wed, Thu, Fri and time is from 09:00 to 15:00 in 24 h format.
But I want in addition add holiday:
For example "*if day and month is equal with 01.01 (1 January) * then will be "else" conditions.
html
<span id="date_time"></span>
<script type="text/javascript">window.onload = date_time('date_time');</script>
Script
function date_time(id)
{
date = new Date;
year = date.getFullYear();
month = date.getMonth();
months = new Array('Janvāris', 'Februāris', 'Marts', 'Aprīlis', 'Maijs', 'Jūnījs', 'Jūlījs', 'Augusts', 'Septembris', 'Oktobris', 'Novembris', 'Decembris');
d = date.getDate();
day = date.getDay();
days = new Array('Svētdiena', 'Pirmdiena', 'Otrdiena', 'Trešdiena', 'Ceturtdiena ', 'Piektdiena', 'Sestdiena');
h = date.getHours();
if(h<10)
{
h = "0"+h;
}
m = date.getMinutes();
if(m<10)
{
m = "0"+m;
}
s = date.getSeconds();
if(s<10)
{
s = "0"+s;
}
if ((day == 1 || day == 2 || day == 3 || day == 4 || day == 5 ) && h >= 9 && h <= 14) {
result = 'Ir '+days[day]+' '+year+'.'+d+'.'+months[month]+' '+h+':'+m+':'+s+' ēdnīca ir atvērta!' ;
document.getElementById("date_time").style.backgroundColor = '#27ae60';
} else {
result = 'Ir '+days[day]+' '+year+'.'+d+'.'+months[month]+' '+h+':'+m+':'+s+' ēdnīca ir slēgta!' ;
document.getElementById("date_time").style.backgroundColor = 'rgba(231, 76, 60, 0.85)';
}
P.S I have this error
TypeError: timeDiv is null [Learn More]
checkTime file:///D:/Dators/Desktop/laiks/script.js:24:5
<anonymous> file:///D:/Dators/Desktop/laiks/script.js:28:66
But I want in addition add holiday: For example "*if day and month is equal with 01.01 (1 January) * then will be "else" conditions.
if (month !== 0 && d !== 1) {
// ...
}
else {
// Only executes on januari first
}

I need to validate the format of time AM|PM

I have this code but its not working.How can i check if time is in AM or PM.
var time = $(id1).val();
var hrs = Number(time.match(/^(\d+)/)[1]);
var mnts = Number(time.match(/:(\d+)/)[1]);
var format = time.match(/\s(.*)$/);
//alert(format);
//alert(time);
if (format == "PM" && hrs < 12) {
hrs = hrs + 12;
}
if (format == "AM" && hrs == 12) hrs = hrs - 12;
You made a copy/paste mistake from my answer from three years ago (convert 12-hour hh:mm AM/PM to 24-hour hh:mm)
var format = time.match(/\s(.*)$/)[1];
Don't forget [1]

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

Categories

Resources