0

<script type="text/javascript">

       $(function() {
            var hours = new Date().getHours();
            var minutes = new  Date().getMinutes();
            console.log(hours);
            console.log(minutes);
            if(hours >= 10 && minutes >=30) {
                 $("#enableOvertime").hide(); // button not hiding
      
            }
       });
       
      
    </script>
<button type="submit" id="enableOvertime">Enable Overtime </button>

I'm currently not getting what is the error with it. using console.log display the correct time and minutes. butid does not hide the button. I want hide the button after 10:30 am every day. any help would be appreciated.

str
  • 42,689
  • 17
  • 109
  • 127
Rakesh
  • 213
  • 6
  • 22

3 Answers3

2

Your condition is wrong. It only captures the second half of each hour (e.g. 10:30-10:59, 11:30-11:59, 12:30-12:59 and so on). You have to change your code to this:

if (hours >= 10 && minutes >= 30 || hours >= 11) {
str
  • 42,689
  • 17
  • 109
  • 127
0

You need to hide the button when the time is after 10:30 in the morning, then you can do something like.

Your logic will fail, when the time is 11:00 because even though the hours is > 10 the minutes is < 30

$(function() {
  var dt = new Date();
  var hours = dt.getHours();
  var minutes = dt.getMinutes();
  console.log(hours);
  console.log(minutes);
  var time = hours * 60 + minutes;
  $("#enableOvertime").toggle(time < 630 || time >= 1110); // button not hiding

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="submit" id="enableOvertime">Enable Overtime</button>

Note: Also note that taking the time from the client side to make such a decision is wrong as the client time can be changed at any time by the user

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 1
    We are converting the time to a minute format and is comparing that like `10:30` is the same as `630` minutes, then the current time also is converted to the same using the formula `hours * 60 + minutes` and those values are comparedc – Arun P Johny Apr 21 '16 at 05:47
  • can you please help me can also do the same for other time also like 6:30 in the evening – Rakesh Apr 21 '16 at 05:47
  • as per you explained, 10 hour * 60 min = 600 min (converting to min)+30(random min) = total 630min ok i got it. but when comapring (630min < 630min) it is not lesser it becomes equal. but it still working how? – Rakesh Apr 21 '16 at 05:55
  • @Rakesh when you pass `true` to the `toggle` method it will show the element, when you pass `false`, it hides the element – Arun P Johny Apr 21 '16 at 05:56
  • i'm not passing any true or false to the toggle – Rakesh Apr 21 '16 at 05:59
  • @Rakesh what is the result of `600 < 630`, If you ask me it will be a boolean value true/false – Arun P Johny Apr 21 '16 at 06:06
  • @Rakesh `$("#enableOvertime").toggle(time < 630 || time >= 1110);` – Arun P Johny Apr 21 '16 at 06:35
0

you can do something like this

Date.parse('01/01/2011 10:20:45') > Date.parse('01/01/2011 5:10:10')

reference compare two time string

Community
  • 1
  • 1
Dhaval Pankhaniya
  • 1,996
  • 1
  • 15
  • 26