0

The application must display the message whether the gate is open or closed depending on the current time of the day.

The gate opens between 8:00am and before 9:30am OR after 5:50pm and before 9:00pm while the rest of the time the gate remains closed. I want to display the message based on the time input. The below code output is unexpected and incorrect.

function isValid(date, h1, m1, h2, m2) {
        var h = date.getHours();
        var m = date.getMinutes();
        return (h1 < h || h1 == h && m1 <= m) && (h < h2 || h == h2 && m <= m2);
    }


function a() {
        var current = new Date('2020-01-03 09:31:00');
        if ((isValid(current, 8, 0, 9, 30)) || (isValid(current, 5, 50, 21, 0))) {
            return 'Gate is open'
        } else {
            return 'Please come after 8:00am and before 9:30am OR after 5:50pm and before 9:00pm';
        }
}a();
4
  • Does this answer your question? compare timestamps in javascript
    – Dan O
    Commented Jan 3, 2020 at 14:12
  • No. What I expect is to display Gate is open or the else message depending on the input time. The input time here is 2020-01-03 09:31:00 which must display "Please come after..." Commented Jan 3, 2020 at 14:43
  • that link shows you exactly how to compare the current time against some other arbitrary time, which is exactly what you asked.
    – Dan O
    Commented Jan 3, 2020 at 20:48
  • Parsing of unsupported string formats is implementation dependent. new Date('2020-01-03 09:31:00') returns an invalid date in at least one current browser, see Why does Date.parse give incorrect results?
    – RobG
    Commented Jan 4, 2020 at 4:11

2 Answers 2

2

You've have to use 17 instead of 5 in the pm date

function isValid(date, h1, m1, h2, m2) {
  var h = date.getHours();
  var m = date.getMinutes();
  return (h1 <= h && m1 <= m) && (h <= h2 && m <= m2);
}


function a() {
  var current = new Date('2020-01-03 09:31:00');
  if ((isValid(current, 8, 0, 9, 30)) || (isValid(current, 17, 50, 21, 0))){
    return 'Gate is open'
  } else {
    return 'Please come after 8:00am and before 9:30am OR after 5:50pm and before 9:00pm';
  }
}
1
  • How do I make a time comparison? What I expect is to display Gate is open or the else message depending on the input time. The input time here is 2020-01-03 09:31:00 which must display "Please come after..." Commented Jan 3, 2020 at 14:44
0

You can use simply Date object as int:

function isValid(date, h1, m1, h2, m2) {
        var h = date.getHours();
        var m = date.getMinutes();

        var d1 = new Date(date);
        d1.setHours(h1);
        d1.setMinutes(m1);

        var d2 = new Date(date);
        d2.setHours(h2);
        d2.setMinutes(m2);

        return (date-d1)*(date-d2) < 0;
    }

2
  • No. What I expect is to display Gate is open or the else message depending on the input time. The input time here is 2020-01-03 09:31:00 which must display "Please come after..." Commented Jan 3, 2020 at 14:44
  • You can set the hours and minutes in one go: d1.setHours(h1, m1). ;-)
    – RobG
    Commented Jan 4, 2020 at 4:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.