-2
var startDateTime = '15.04.2019 00:15';
var endDateTime = '17.05.2019 18:35';

var checkDateTime = '16.04.2019 13:15';

function(checkDateTime, startDateTime, endDateTime) {

// need codes to return true or false,.
// check "checkDateTime" is between "startDateTime" to "endDateTime"

}
Mizhar Raja
  • 174
  • 2
  • 12
  • 1
    You've tagged this with [tag:momentjs]; did you try using it? –  Apr 25 '19 at 06:11
  • Very similar to: [13291661](https://stackoverflow.com/questions/13291661/check-if-a-date-within-in-range). Almost a dupe. – RichS Apr 25 '19 at 06:14
  • Possible dupe of [48405762](https://stackoverflow.com/questions/48405762/js-check-if-multiple-dates-are-within-range). If the array of dates were size 1...that would answer this.. – RichS Apr 25 '19 at 06:15
  • 2
    You need to provide a [mcve] (And no, having an empty function does not count) – Alon Eitan Apr 25 '19 at 06:16

4 Answers4

4

Try this code:

var startDateTime = getDate('15.04.2019 00:15');
var endDateTime = getDate('17.05.2019 18:35');

var checkDateTime = getDate('16.04.2019 13:15');

function isBetween(checkDateTime, startDateTime, endDateTime) {

    return (checkDateTime >= startDateTime &&  checkDateTime <= endDateTime);

}

function toDate(str){
  var [ dd, MM, yyyy, hh, mm ] = str.split(/[. :]/g);
  return new Date(`${MM}/${dd}/${yyyy} ${hh}:${mm}`);
}

console.log(isBetween(checkDate,startDate,endDate));

To compare it one time falls between a time interval on the same day use: -

var startTime = "00:35";
var endTime = "18:15";
var checkTime = "13:00";

function getMinutes(timeString){
    let [hh, mm] = timeString.split(":");
    return parseInt(hh)*60 + parseInt(mm);
}

function isTimeBetween(checkTime,startTime,endTime){
      checkTime = getMinutes(checkTime);
      return (checkTime >= getMinutes(startTime) && checkTime <= getMinutes(endTime));
}
console.log(isTimeBetween(checkTime,startTime,endTime));
RK_15
  • 929
  • 5
  • 11
1

You can use new Date().getTime() to get the number of milliseconds since the Unix Epoch. So that you can compared the date/time with the result from this function. You can do sth like that:

return new Date(startDateTime).getTime() <= new Date(checkDateTime).getTime() <= new Date(endDateTime).getTime();

Check this out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

Elvis Wong
  • 348
  • 2
  • 8
  • Almost.. When I test this function, it also returns true if the date is out of the range. When I set `checkDateTime` to '06/16/2019 13:15' (out of range), the function returns `true`, but should be `false`. I think the conditional just needs to be separated by an &&. – RichS Apr 25 '19 at 06:47
1

I would suggest to check if your checkDateTime is greater than your startDateTime and less then endDateTime.

function checkDateTime(checkDateTime, startDateTime, endDateTime) {
  return (new Date(startDateTime) >= new Date(checkDateTime))
      && (new Date(checkDateTime) <= new Date(endDateTime));
}
marcobiedermann
  • 4,317
  • 3
  • 24
  • 37
  • If you go for this solution, I would also recommend to store `new Date(checkDateTime)` in a variable to improve the performance. I left it out by intention to avoid any unnecessary confusion – marcobiedermann Apr 25 '19 at 06:22
1

Here is yet another option which adds the method directly to the Date prototype:

var startDateTime = new Date('04/15/2019 00:15');
var endDateTime = new Date('05/17/2019 18:35');

var checkDateTime = new Date('04/16/2019 13:15');
var outOfRangeDate_EARLY = new Date('01/16/2019 13:15');
var outOfRangeDate_LATE = new Date('06/16/2019 13:15');

Date.prototype.inRange = function(startDate, endDate){
  var this_ms = this.getTime();
  return ( this_ms >= startDate.getTime() && this_ms <= endDate.getTime() )
}

/* Tests */
console.log('expected: true', 'actual:', checkDateTime.inRange(startDateTime, endDateTime))
console.log('expected: false', 'actual:', outOfRangeDate_EARLY.inRange(startDateTime, endDateTime))
console.log('expected: false', 'actual:', outOfRangeDate_LATE.inRange(startDateTime, endDateTime))

This way, with any date you have var someDate, you can just call someDate.inRange(startDate, endDate). Sometimes, however, messing with the native prototypes can come back to haunt you if not careful. If so, having a separate function as answered by the others is very good.

Lastly, it's very important that the date strings are formatted properly before creating the Date objects, otherwise you'll encounter Invalid Date a lot. I hope this helps.

RichS
  • 913
  • 4
  • 12