-2

I've done some searches but couldn't find anything similar.

I want to disable the button if the Date is on the 15th of the month in 9AM and resume it on the 16th 9AM and I also want to disable it on the last day of the month still 9AM and resume it on the 1st day of the next month in 9AM.

Your help is very much appreciated. Thank you!

Here is what I've tried so far and its not working.

var thisDay = new Date().getDate();
    var thisTime = new Date().getHours();

    if (thisTime>=9 && thisDay==15) {
        $('#AddMenu').attr("disabled", true);
    }
azis
  • 29
  • 1
  • 6

1 Answers1

0

Given the way you have written it nothing will work properly. You are trying to see if thisTime is between 9 and midnight so you should use && instead of ||. To disable a button you use the .prop("disabled", true) property. You don't need a click modifier as you want your program to check this script as soon as it loads. To run something once your page is loaded include a script tag in the body and call your function there. I highly recommend you look into learning more of the basics of Javascript and Jquery if you plan on taking on projects like this in the future. With what you have now you won't produce results.

This is the solution you are looking for:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>DOM Example</title>
        <script src="jquery-3.1.1.min.js">
        </script>
        <script>
            var thisDay = new Date().getDate();
            var thisTime = new Date().getHours();

            function checkButton()
            {
                if ((thisTime >=9 && thisTime<=24) && (thisDay>=15 && thisDay<=16)) 
                {
                    $("#add").prop("disabled", true);
                }
                printAlert();

            }

            function printAlert() {
                window.alert('We are not accepting entries right now.');
            }
        </script>
    </head>

    <body>
        <p id="firstParagraph">This is the first paragraph</p>
        <button id="add">Button</button>

        <script>
            checkButton();
        </script>
    </body>
</html>
Reg Smith
  • 166
  • 1
  • 15
  • We will help you alter code if you supply some, but if you don't offer any we won't write it for you. – Reg Smith Feb 16 '17 at 16:03
  • You added it after my comment. I have looked at your code and you are going to have issues far beyond not being able to disable the button. Every || in your if statement should be a &&. Adding a class of disabled just gives it a class named disabled. You should use .prop('disabled', true). You really need to look into learning the basics and the usage of modifiers and elements in jquery before you try to tackle something of this difficulty. – Reg Smith Feb 16 '17 at 16:49
  • I edited my code now. It disables the button now. My problem right now is how to check if it's the end of the month – azis Feb 16 '17 at 16:56
  • Here is a perfect thread relating to that. http://stackoverflow.com/questions/13571700/get-first-and-last-date-of-current-month-with-javascript-or-jquery – Reg Smith Feb 16 '17 at 17:13