K20GH
K20GH

Reputation: 6263

Momentjs Range returning False

I'm trying to use Moment to find out if a date is inbetween 2 dates using the Moment Range plugin: https://github.com/rotaready/moment-range#contains

For some reason, my code below is always throwing out False. Here is an example of the output:

START: 2015-01-01 00:00:00 END: 2017-08-01 00:00:00 CLOSED: 2017-06-08 17:59:56 DECISION: false

You can clearly see here that the closed date falls between the start and end date, yet I am told it does not

Here is my code:

let end = moment('2017-08-01 00:00:00').format('YYYY-MM-DD HH:mm:ss')
let start = moment('2015-01-01 00:00:00').format('YYYY-MM-DD HH:mm:ss')
let closedDate = moment('2017-06-08 17:59:56').format('YYYY-MM-DD HH:mm:ss')
let range = moment.range(start, end)

console.log(`START:`,start,'END:',end,'CLOSED:',closedDate,'DECISION:',range.contains(closedDate));

Upvotes: 0

Views: 997

Answers (1)

K20GH
K20GH

Reputation: 6263

I managed to fix this using the within function, and also changing the how I format the moment objects

let end = moment('2017-08-01 00:00:00', 'YYYY-MM-DD HH:mm:ss')
let start = moment('2015-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss')
let closedDate = moment('2017-06-08 17:59:56','YYYY-MM-DD HH:mm:ss')
let range = moment.range(start, end)
console.log(`START:`,start,'END:',end,'CLOSED:',closedDate,'DECISION:',closedDate.within(range));

Upvotes: 1

Related Questions