Reputation: 594
I'm writing a code that has date time in a json, And I want to filter the json and show the data where date
from datetime
is greater than today's date
.
Here is my code.
var data = [{
datetime: "2021-08-09T06:00:00.000Z",
id: "1"
}, {
datetime: "2021-06-07T02:00:00.000Z",
id: "2"
}, {
datetime: "2021-08-04T11:00:00.000Z",
id: "3"
}, {
datetime: "2021-08-04T10:00:00.000Z",
id: "4"
}, {
datetime: "2021-08-05T12:55:00.000Z",
id: "5"
}, {
datetime: "2020-08-10T13:30:00.000Z",
id: "6"
}]
data = data.filter(item=> { return new Date(item.datetime).getDate() > new Date().getDate()});
console.log(data);
it should print every thing apart from id 3, 4 and 6
. Also when I do a getDate()
, I think it only considers DD
from MMDDYYYY
, that's why 6
is getting printed. what would be the best way to achieve this?
Upvotes: 0
Views: 426
Reputation: 631
data = data.filter(item => {
const date = new Date(item.datetime);
const today = new Date();
return date.getUTCFullYear() * 10000 + date.getUTCMonth() * 100 + date.getUTCDate() > today.getUTCFullYear() * 10000 + today.getUTCMonth() * 100 + today.getUTCDate();
});
Upvotes: 0
Reputation: 2356
There seems to be a typo:
data = data.filter(item=> { return new Date(item.datatime).getDate() > new Date().getDate()});
// ^
// datetime
Upvotes: 2