1

I want to hide submit button a number of times in a day.

Below code sometime works, sometime not works. Yesterday code worked but today code not working. I am unable to understand where the problem is.

<script>
 window.addEventListener("load", function() {
  // Check time and update the button's state every second.
  setInterval(updateSubmitButtonState, 1000);
}, false);

function updateSubmitButtonState() {
var timezone = "Asia/Calcutta";
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var t =currentTime.getHours()  + ":" + currentTime.getMinutes();

 if (t >= '10:00' && t <= '10:20' || 
    t >= '11:00'  && t <= '11:20' || 
    t >= '13:00'  && t <= '13:20' || 
    t >= '15:15'  && t <= '15:25' || 
    t >= '17:30'  && t <= '17:40' || 
    t >= '19:00'  && t <= '19:10' || 
    t >= '20:30'  && t <= '20:40' ) {
    $("#submit").css("display", "none");
  } else {
    $("#submit").css("display", "block");
  }
}
</script>
<input type="submit" id="submit" name="submit">
dipak dutta
  • 274
  • 2
  • 13
  • 1
    `t >= '10:00' .....` seems to be only string comparison where as you need to compare the time . Please check this link for time comparison https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss – brk Nov 20 '17 at 06:05
  • 1
    You are comparing a `number` with `string`. – gurvinder372 Nov 20 '17 at 06:06

3 Answers3

2

It is because you are converting to the string and it tries to order lexicographically. Ex.: 17:07 will be converted like "17:7" so this will be greater than "17:30". I recommend to use this function:

function timeToMinutes(h, m) {
  return h * 60 + m;
}

then you can compare like

timeToMinutes(h, m) > timeToMinutes(10, 0) ...

Try this sample code

function timeToMinutes(h, m) {
  return h * 60 + m;
}

var currentTime = new Date();
var mins = timeToMinutes(currentTime.getHours(), currentTime.getMinutes());
alert(mins >= timeToMinutes(19, 20) && mins <= timeToMinutes(19, 30));
Bahriddin Abdiev
  • 328
  • 1
  • 14
2

You are checking with string, am suggesting you to change time to millisecond.

try this out.

<script>
 window.addEventListener("load", function() {
  // Check time and update the button's state every second.
  setInterval(updateSubmitButtonState, 1000);
}, false);
function convertMilliSec(hr, min){
    return (parseInt(hr) * (60000 * 60)) + (parseInt(min) * 60000)
} 
function updateSubmitButtonState() {
var timezone = "Asia/Calcutta";
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var t = convertMilliSec(currentTime.getHours(), currentTime.getMinutes());
    console.log(t);

 if (t >= convertMilliSec(10,00) && t <= convertMilliSec(10,20) || 
    t >= convertMilliSec(11,00) && t <= convertMilliSec(11,20) ||  
    t >= convertMilliSec(14,00) && t <= convertMilliSec(14,34) || 
    t >= convertMilliSec(13,00) && t <= convertMilliSec(13,20) || 
    t >= convertMilliSec(15,00) && t <= convertMilliSec(15,20) || 
    t >= convertMilliSec(17,00) && t <= convertMilliSec(17,20) || 
    t >= convertMilliSec(19,00) && t <= convertMilliSec(19,20) || 
    t >= convertMilliSec(20,00) && t <= convertMilliSec(20,20) ) {
    $("#submit").css("display", "none");
  } else {
    $("#submit").css("display", "block");
  }
}
</script>
Arun
  • 142
  • 1
  • 1
  • 9
1

You need either to convert hours and minutes into two digit number (zero lead) like this:

var hours = currentTime.getHours();
hours = hours > 9 ? hours : '0'+hours.toString();

var minutes = currentTime.getMinutes();
minutes= minutes > 9 ? minutes: '0'+minutes.toString();

var t = hours + ":" + minutes;

Or you need to convert all of them into Date object:

var t = currentTime.getHours()  + ":" + currentTime.getMinutes();
var dObj = new Date('01/01/2017 '+t+':00');

Then when comparing you also compare with Date object:

if(dObj >= new Date('01/01/2017 10:00:00') && t <= new Date('01/01/2017 10:20:00') .....

Update:
Here's a JSFiddle for the second method:
https://jsfiddle.net/02z5hbo1/2/

Dabbas
  • 3,112
  • 7
  • 42
  • 75