Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What I Want Is Whenever The Page Loads The Button Should Get Disabled If The Current Time Of the Day Is Greater Than 10PM

What I have tried:

I Did Try Some Of The Java Scripts But It Isn't Working
ASP.NET
<script type="text/javascript" defer="defer">
<!--
        var enableDisable = function () {
            var UTC_hours = new Date().getUTCHours() + 1;
            if (UTC_hours > 22 && UTC_hours < 8) {
                document.getElementById('Button1').disabled = true;
            }
            else {
                document.getElementById('Button1').disabled = false;
            }
        };
        setInterval(enableDisable, 1000 * 60);
        enableDisable();
// -->
</script>
Posted
Updated 8-May-16 20:31pm
v2
Comments
Philippe Mori 7-May-16 16:12pm    
Put your code in a code block.

Take another look at your if condition:
JavaScript
if (UTC_hours > 22 && UTC_hours < 8)

Whatever value is in UTC_hours, it cannot be both "greater than 22" and "less than 8" at the same time.

You probably mean to use an "OR" instead of an "AND":
JavaScript
if (UTC_hours > 22 || UTC_hours < 8)


However, this will only disable the button at 11 PM. At 10:59 PM, the hour is still 10, so it's not greater than 10.

If you want to disable the button at 10 PM, you need to change the first condition:
JavaScript
if (UTC_hours >= 22 || UTC_hours < 8)


You should also bear in mind that this is using UTC time based on the user's clock. It won't use your server's time-zone; it won't use your client's time-zone; and if the client's clock is wrong, the code will enable/disable the button at the wrong time.
 
Share this answer
 
Comments
ridoy 8-May-16 13:36pm    
my 5.
The time to trigger the handler should be variable, depending on the time of execution of the code setting the time, but you use the constant. I see no reason to check up the rest; first fix this problem. You already got real time; so do the rest.

I would also use setTimeout instead of setInterval:
WindowTimers.setTimeout() — Web APIs | MDN[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900