简体   繁体   中英

Check if date/time is BETWEEN two other date/times Javascript

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"

}

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));

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

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));
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM