0

i am trying to filter an array based on dates.

var items = [
 {
   createdBy: "suriyan"
   product: "tv"
   from: "2019-10-15T18:30:00.000Z"
   to: "2019-10-29T18:30:00.000Z"
 },
 {
  createdBy: "suriyan"
  product: "phone"
  from: "2019-10-19T18:30:00.000Z"
  to: "2019-10-29T18:30:00.000Z"
 }
]

filtered:[];

for(let i=0;i<this.items.length;i++){
  const now = new Date();
  if(now>this.items[i].from){
    this.filtered.push(this.items[i])
  }
}
console.log(this.filtered);

but this is not working for me. Can someone help me on this. Thanks in advance

Shubh
  • 7,299
  • 3
  • 15
  • 37
suriyan
  • 33
  • 1
  • 6

4 Answers4

0

You are comparing Date with a String value, so convert it to Date first then compare:

if(now > new Date(this.items[i].from){
    this.filtered.push(this.items[i])
}

Using RxJS, you dont need to loop, so try this as well:

var items = [{
           createdBy: "suriyan",
           product: "tv",
           from: "2019-10-15T18:30:00.000Z",
           to: "2019-10-29T18:30:00.000Z",
        },
        {
          createdBy: "suriyan",
          product: "phone",
          from: "2019-10-21T18:30:00.000Z",
          to: "2019-10-29T18:30:00.000Z",
        }
    ]
    
filtered = [];
const now = new Date();
this.filtered = items.filter(item => now > new Date(item.from));
console.log(this.filtered);
Mustahsan
  • 3,370
  • 1
  • 11
  • 27
  • when i do this ``` { createdBy: "suriyan", product: "phone", from: "2019-10-20T18:30:00.000Z", to: "2019-10-29T18:30:00.000Z", } let data = items.filter(ele => ele.from > new Date().toISOString() ); console.log(new Date().toISOString()); console.log("filtered data is ",data)```it is giving me wrong answer. here the answer should be empty array but still it gives me the above object..... because the above the condition is (Todays date > Todays date ) so it should return an empty array as the condition is false , but still it returns true. Could you help me with this – suriyan Oct 21 '19 at 08:58
  • you want all those items whose date is less than todays date right? – Mustahsan Oct 21 '19 at 09:47
  • i need to filter items based on todays date, for example if the from date is equal to todays date (new Date(item.from) >= now) but if i run (new Date(item.from) > now) this still it gives me items but logically i should get an empty array – suriyan Oct 21 '19 at 10:22
0

Try this.You can use Array.filter() directly.With new Date() converted to ISO

var items = [{
    createdBy: "suriyan",
    product: "tv",
    from: "2019-10-15T18:30:00.000Z",
    to: "2019-10-29T18:30:00.000Z",
  },
  {
    createdBy: "suriyan",
    product: "phone",
    from: "2019-10-19T18:30:00.000Z",
    to: "2019-10-29T18:30:00.000Z",
  }
]

let data = items.filter(ele => ele.from > new Date().toISOString());
console.log(data)
console.log(new Date().toISOString())// current date time
Shubh
  • 7,299
  • 3
  • 15
  • 37
  • thanks for your answer but when i do this ``` { createdBy: "suriyan", product: "phone", from: "2019-10-20T18:30:00.000Z", to: "2019-10-29T18:30:00.000Z", } let data = items.filter(ele => ele.from > new Date().toISOString() ); console.log(new Date().toISOString()); console.log("filtered data is ",data)```it is giving me wrong answer. here the answer should be empty array but still it gives me the above object...... – suriyan Oct 20 '19 at 10:22
  • because the above the condition is (Todays date > Todays date ) so it should return an empty array as the condition is false , but still it returns true. Could you help me with this – suriyan Oct 20 '19 at 10:26
  • You can do `ele.from >new Date().toISOString()` – Shubh Oct 20 '19 at 11:18
  • Yes that is what i did let data = items.filter(ele => ele.from > new Date().toISOString() ); – suriyan Oct 21 '19 at 08:46
  • Check updated answer @suriyan.Its giving empty array now – Shubh Oct 21 '19 at 08:50
  • You are getting the empty output array ,above what else you want?.Did you run the above snippet? – Shubh Oct 21 '19 at 09:39
  • yes i checked when you change the from date to 21.10.2019 ("2019-10-21T18:30:00.000Z") and run the code it is still giving me an array with one object.But as per condition today >today so it should be false right ... – suriyan Oct 21 '19 at 09:43
  • i am running this ''' var items = [{ createdBy: "suriyan", product: "tv", from: "2019-10-15T18:30:00.000Z", to: "2019-10-29T18:30:00.000Z", }, { createdBy: "suriyan", product: "phone", from: "2019-10-21T18:30:00.000Z", to: "2019-10-29T18:30:00.000Z", } ] let data = items.filter(ele => ele.from > new Date().toISOString()); console.log(data) ''' – suriyan Oct 21 '19 at 09:44
  • That's because of the time zone .This snippet gives the date time according to the timezone of server its running.Check for new Date() I printed. Try using some past time as per the current time it will work.@suriyan .And hours and minutes difference also matters in this case. – Shubh Oct 21 '19 at 09:47
  • I understand in snippet but i am trying in my local server as well . So it should work fine in my local server right but in local server also it returns an array with one object – suriyan Oct 21 '19 at 10:24
  • new Date().toISOString() it gives me "2019-10-21T10:28:50.209Z" (TODAYS DATE + TIME) but how can we get only date and use it in condition and get the desired result. – suriyan Oct 21 '19 at 10:30
  • ok yes Shubh timezone should also be an issue here. Thanks for updating me about this timezone – suriyan Oct 21 '19 at 11:41
0
var items = [{
    createdBy: "suriyan",
    product: "tv",
    from: "2019-10-15T18:30:00.000Z",
    to: "2019-10-29T18:30:00.000Z",
  },
  {
    createdBy: "suriyan",
    product: "phone",
    from: "2019-10-19T18:30:00.000Z",
    to: "2019-10-29T18:30:00.000Z",
  }
]

let data = items.filter(ele => new Date().toISOString() > ele.from);
console.log(data)
Jainil
  • 1,352
  • 1
  • 19
  • 23
-1

GO WITH THIS ONE

 const items = [
        {
            createdBy: 'suriyan',
            product: 'tv',
            from: '2019-11-15T18:30:00.000Z',
            to: '2019-11-29T18:30:00.000Z',
        },
        {
            createdBy: 'suriyan',
            product: 'phone',
            from: '2019-10-19T18:30:00.000Z',
            to: '2019-10-29T18:30:00.000Z'
        }
    ];

    items.sort((a, b) => {
        // Turn your strings into dates, and then subtract them
        // to get a value that is either negative, positive, or zero.
        return new Date(b.from) - new Date(a.from);
    });

    console.log(items);
sibabrat swain
  • 1,194
  • 5
  • 18