K20GH
K20GH

Reputation: 6263

Moment is time between 2 times, regardless of date

I'm trying to figure out how to do a simple IF statement, to figure out if the provided date is between 08:00 and 20:15, regardless of the date

var obj = { "Date/Time": "2018-09-30 23:51:09" };

const date = moment(obj['Date/Time']).format("HH:mm:ss");
const start_time = "08:00:00";
const end_time = "20:15:00";

if (moment(date).isBetween(start_time, end_time)) {
  console.log("yes")
}

My code above though fails with the following error:

Error at Function.createFromInputFallback
(https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js:17:482)

Any suggestions?

Upvotes: 1

Views: 178

Answers (4)

Salman Arshad
Salman Arshad

Reputation: 272096

Assuming all three strings are in HH:mm:ss format you can simply compare them as strings:

var date       = "23:51:09";
var start_time = "08:00:00";
var end_time   = "20:15:00"; 
if (start_time <= date && date <= end_time) {
    console.log("yes");
}

And if there is a possibility of having midnight between start and end time then:

var date       = "23:51:09";
var start_time = "20:00:00";
var end_time   = "04:00:00"; 
if (
    (start_time <= end_time && (start_time <= date && date <= end_time)) ||
    (start_time >  end_time && (start_time <= date || date <= end_time))
) {
    console.log("yes");
}

Upvotes: 2

blov80
blov80

Reputation: 1

Why don't you try something like this?

    const date = new Date('2018-09-30 23:51:09');
    const start_time = new Date('2018-09-30 8:00:00');
    const end_time = new Date('2018-09-30 20:15:00');     

    if (start_time<date && date<end_time) {
      console.log('yes');
    }

Upvotes: 0

MrPickles
MrPickles

Reputation: 949

It looks like yyou will need to get the time for start and end with a moment commpatible time.

const start_time = moment(obj['Date/Time']).hour(8).startOf('hour');
const end_time = moment(obj['Date/Time']).hour(20) minute(15).startOf('minute');

if (moment(date).isBetween(start_time, end_time)) {
    console.log('WIN!');
}

if you are still getting the error it will be that the format of the date is not what moment it looking for and you will need to tell it the format with the 2nd arg for moment(DATE, FORMAT)

Upvotes: 0

31piy
31piy

Reputation: 23859

Firstly, you need to construct the moment object correctly using moment(string, string) constructor in the if expression since the date string is not in ISO format.

Secondly, you can construct the start_time and end_time as moment objects as well, so that you can directly compare the time values of the three objects. See the demo below:

var obj = {
  'Date/Time': '2018-09-30 09:51:09'
}

const date = moment(obj['Date/Time']).format("HH:mm:ss");
const start_time = moment("08:00:00", 'HH:mm:ss');
const end_time = moment("20:15:00", 'HH:mm:ss');

if (moment(date, 'HH:mm:ss').isBetween(start_time, end_time)) {
  console.log("yes")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 0

Related Questions