HViet
HViet

Reputation: 5

(JS) Enable Button during a certain period of time in a day

I've been trying to enable a button only in a certain period of time in a day, but I can't make it work..

What I've been trying to do: The button should be enabled from 17:00 o'clock to 22:00 o'clock (GMT +1). I wrote my script with the aid of sites where people were requesting a similar system, but I can't make it work.. I hope that you can help me

Sorry for my english^^

Here is the script:

<input class="submit" type="submit" id="checktime" value="Check"/>

<script type="text/javascript" defer="defer">
<!-- 
var enableDisable = function(){
var UTC_hours = new Date().getUTCHours() +1;
if ((UTC_hours == 17) && (UTC_hours == 18) && (UTC_hours == 19) && (UTC_hours == 20) && (UTC_hours == 21) && (UTC_hours == 22)){
document.getElementById('checktime').disabled = false;
else
document.getElementById('checktime').disabled = true;
}
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>

Upvotes: 0

Views: 1848

Answers (2)

Marko
Marko

Reputation: 2734

You had missing brackets and your id was wrong, this should work.

<input class="submit" type="submit" id="checktime" value="Check"/>

<script type="text/javascript" defer="defer">
<!-- 
var enableDisable = function(){
    var UTC_hours = new Date().getUTCHours() +1;
    if (UTC_hours > 16 && UTC_hours < 22){
        document.getElementById('checktime').disabled = false;
    }
    else
    {
        document.getElementById('checktime').disabled = true;
    }
};
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>

Upvotes: 1

richsilv
richsilv

Reputation: 8013

The if statement isn't going to work like that; you're testing if the hour is 17 AND 18 AND 19 AND..., which is obviously never going to be the case as it can't be all of those hours at once. It should be:

if (UTC_hours > 16 && UTC_hours < 22)

That will ensure that it's at least 5pm, but before 10pm.

Upvotes: 1

Related Questions