SOLVED - If (time) then

Hi guys!
I want to hide an element when it’s a certain time of the day, except for weekends. So basically, I want the $w(‘#XXX’).hide code to run on Mon to and incl. Fri, between 8:30 AM and 3:30 PM. Is this possible?
Thanks!!!

Edit: The solution is:

let nowTime = new Date(),
        today = nowTime.getDay(),
        hour = nowTime.getHours();
 if (today >= 1 && today <= 5 && hour >= 8 && hour <= 15) {
        $w("#chat").show;
    }

= 1 (from and incl. Monday) && <= (to and incl. Friday)
= 8 (from 8:00 (8 am)) && <= 15 (to and incl. 15:00 (3 pm))
You can also add the minutes the same way.

Yes. It is possible.
You can get the current time, day, hours using JavaScript Date methods:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Methods

Awesome!

But how do I use it exactly in the code? Could you give an example maybe? Would really help!

@webmasterq

if(new Date().getDay() === 2){
$w("#myElement").hide();
}

(the days of the week are from 0-6)

@webmasterq sure. And you can also use && and || as AND-OR:

let today = new Date().getDay();
if(today === 2 || today === 5){
//code
}

Sunday is 0
Saturday is 6

P.S. needless to say that in case of days of the week, there’s no use for AND (as it can’t be Sunday and Monday at the same time. But you can use && for day + hour for example.)

Thanks @J. D. ! I now have this code:

let nowTime = new Date(),
        today = nowTime.getDay(),
        hour = nowTime.getHours();
 if (today >= 1 && today <= 5 && hour >= 8 && hour <= 15) {
        $w("#chat").show;
    }

This activates the code from Monday to and incl. Friday, between 8:00 AM and 4:00 PM

Yes, but you don’t need to call new Date() 4 times.
You can do it:

let nowTime = new Date(), today = nowTime.getDay(), hour = nowTime.getHours();
if(today >= 1 && today <= 5 && hour >= 8 && hour <= 15){
//code
}

@jonatandor35 Thank you so much!!!

@webmasterq you’re welcome.
P.S.
The difference between the code you suggested earlier (and meanwhile changed it) and mine is very small. But it may be significant in very rare cases:
let’s say that the new Date().getDay() ran at Sunday 23:59:999 and the following new Date().getHours() ran at Monday 00:00:000 , then it will be treated as Sunday 00:00:000 (which is 24 hours earlier). It’s super rare, but if you can prevent it by running the new Date() one time only, that’s better.

@jonatandor35 Okay

Hi, I’m trying to do something a little similar to this but with a dataset values involved…

I have my Wix Events collection connected to 3 text elements on the page to display the next scheduled event. I want #watchSessionButton to show when the event is live.

I tried to use the info above with the getCurrentItem API to make this happen but it’s not working. I’m sure I’ve done this completely wrong but not sure where to go from here.

Any help would be appreciated!

$w.onReady(function () {
 //TODO: write your page related code here...
    $w("#eventsDataset").onReady(() => {
 let itemObj = $w("#eventsDataset").getCurrentItem();

 let eventStart = itemObj.start();
 let eventEnd = itemObj.end();

 const today = new Date();

 if (eventStart <= today && eventEnd >= today ) {
            $w("#artistSignupButton").hide();
            $w("#watchSessionButton").show();
        }

    });

});