0

I have an array that has a startTime. I want to get items where startTime in between 2020-12-10 09:30:00 and 2020-12-10 13:20:00. I am using the filter method to get the items from the array.

startTime>'2020-12-10 09:30:00' // is working, returns array with 6 items

but when i add startTime>'2020-12-10 09:30:00' && startTime<'2020-12-10 13:20:00' // returns null

not able to understand why it not filtering the result when I use less than

here is my code

var empStart = empShift.filter(item => {
    return item.startTime>'2020-12-10 09:30:00' && item.startTime<'2020-12-10 
    13:20:00'
    })
mural
  • 29
  • 1
  • 8

1 Answers1

0

Thanks for the response guys I came to know whats my mistake

var empStart = empShift.filter(item => {

return new date(item.startTime).getTime() >'2020-12-10 09:30:00' && new 
date(item.startTime).getTime() <'2020-12-10 13:20:00'

})

new date(//variable).getTime() // solved my issue

mural
  • 29
  • 1
  • 8
  • 1
    That's not going to fix the problem. `getTime()` returns the number of milliseconds since the epoch, and they are being compared with strings. – Heretic Monkey Jan 13 '21 at 20:01
  • I stored date in a variable like new date(item.startTime).getTime() >new date(neededDate).getTime() – mural Jan 13 '21 at 20:33
  • 1
    I read your answer; repeating it doesn't change the problem.So if you run `new Date().getTime()`, it should return a value like `1610570971995`. Do you see how comparing that to the string `'2020-12-10 09:30:00'` doesn't make much sense? Another example: try running `new Date().getTime() < '2321-02-01 13:20:00'` Certainly that should be true? Today is obviously less than February 1st 2321! But it returns false. If, however, you run `new Date().getTime() < new Date('2321-02-01T13:20:00').getTime()`, it does return true, because you're comparing two of the same type of value. – Heretic Monkey Jan 13 '21 at 20:56
  • `new Date('2020-12-10 09:30:00')` creates an invalid date in at least one current browser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 13 '21 at 21:57