Reputation: 21
I am trying to create a JS function hide the HTML element on before 11:00 AM on Monday - Friday and after 03:00:00 PM Monday - Friday and hide the element all day on Saturday && Sunday. Here is what I have so far. I think I am getting there but Time is very tricky. What am I missing here? Thanks!!
jQuery(function() {
/* your code here */
// days of the week
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
// Gather Time
var startTime = "11:00:00 AM";
var endTime = "03:00:00 PM"
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
var ap = "AM";
if (hour > 11) { ap = "PM"; }
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
if (hour < 10) { hour = "0" + hour; }
if (minute < 10) { minute = "0" + minute; }
if (second < 10) { second = "0" + second; }
var currentTime = hour + ':' + minute + ':' + second + " " + ap;
if(n == "Monday" || n == "Tuesday" || n == "Wednesday" || n == "Thursday" || n == "Friday") {
if(startTime > currentTime && currentTime < endTime){
jQuery( "div.rpress_fooditem[data-term-id='2']" ).css({"display":"none"});
}}
else
jQuery( "div.rpress_fooditem[data-term-id='2']" ).css({"display":"block"});
alert("other day");
return currentTime;
});
Upvotes: 1
Views: 82
Reputation: 21
So i was able to figure out instead using 12 hours format i use 24 hours format instead and convert into string and it works fine! Thank you for your help
Upvotes: 0
Reputation: 1431
You are comparing strings in your deciding if, not actual dates...
var startTime = "11:00:00 AM";
var endTime = "03:00:00 PM"
var currentTime = hour + ':' + minute + ':' + second + " " + ap;
if(startTime > currentTime && currentTime < endTime)
UPDATE
I would use Date
then I would change the hours and minutes accordingly for startDate
and for endDate
and for currentTime
I wound not change anything. I hope that make sense.
UPDATE 2
The real question is on what kind of Date are we operate on? Are we doing it in UTC or it depends to the Timezone of the user (taken from browser). If the answers is "based on timezone" than I will advise you to use library like momentjs or something like this because due to change of "Summer Time" that can change overtime you need library that updates that information. I wont elaborate about it more, you can reasearch what the problem is really about in google.
Upvotes: 1