简体   繁体   中英

Filtering JSON data between 2 dates

I have JSON data being returned to me, and I am trying to filter the data between two dates and its not going very well. The code is here and what you see that is commented out is what I have tried already to no avail (the date format is dd-MM-yyyy, incase you are wondering)..

let data = [
    { date : "06-06-2020", toll:1, Province: "Ontario" },
    { date : "06-06-2020", toll:10, Province: "Alberta" },
    { date : "07-06-2020", toll:2, Province: "Ontario" },
    { date : "08-06-2020", toll:2, Province: "Alberta" },
    { date : "09-06-2020", toll:15, Province: "Alberta" },
    { date : "08-06-2020", toll:18, Province: "Ontario" },
    { date : "07-06-2020", toll:11, Province: "Nova Scotia" },
    { date : "07-06-2020", toll:1, Province: "Ontario" },
    { date : "10-06-2020", toll:10, Province: "Manitoba" },
    { date : "11-06-2020", toll:9, Province: "Manitoba" },
    { date : "11-06-2020", toll:3, Province: "Ontario" },
    { date : "07-06-2020", toll:89, Province: "Manitoba" },
    { date : "06-06-2020", toll:90, Province: "Ontario" },
    { date : "06-06-2020", toll:45, Province: "Nova Scotia" },
    { date : "13-06-2020", toll:55, Province: "Ontario" },
    { date : "13-06-2020", toll:1, Province: "Ontario" },
    { date : "13-06-2020", toll:17, Province: "Ontario" },
    { date : "12-06-2020", toll:2, Province: "Nova Scotia" },
    { date : "08-06-2020", toll:8, Province: "Ontario" },
    { date : "08-06-2020", toll:9, Province: "Newfoundland " },
    { date : "06-06-2020", toll:11, Province: "Newfoundland " },
    { date : "12-06-2020", toll:100, Province: "Ontario" },
    { date : "06-06-2020", toll:13, Province: "Ontario" }
];

function GetData(){
    let startDate, endDate;
        startDate = new Date("03-06-2020");
        endDate = new Date("13-06-2020");

    console.log(data.filter(f => f.Province == "Ontario"));

    //let betweenDate = data.filter(f => {
    //    let date = new Date(f.date);
    //    return (f.date >= startDate && f.date <= endDate && f.Province == "Ontario");
    //});
    //
    //let betweenDate = data.filter(f => {
    //    return (f.date >= startDate && f.date <= endDate && f.Province == "Ontario");
    //});

    //console.log(betweenDate);
}

GetData();

"the date format is dd-MM-yyyy, incase you are wondering" → that's the problem. new Date does not know you're using that format. Below, I added a method to convert it to YY-MM-dd .

You also need to compare the date variable you created, not f.date :

 // Minified, but same data as yours let data = [{date:"06-06-2020",toll:1,Province:"Ontario"},{date:"06-06-2020",toll:10,Province:"Alberta"},{date:"07-06-2020",toll:2,Province:"Ontario"},{date:"08-06-2020",toll:2,Province:"Alberta"},{date:"09-06-2020",toll:15,Province:"Alberta"},{date:"08-06-2020",toll:18,Province:"Ontario"},{date:"07-06-2020",toll:11,Province:"Nova Scotia"},{date:"07-06-2020",toll:1,Province:"Ontario"},{date:"10-06-2020",toll:10,Province:"Manitoba"},{date:"11-06-2020",toll:9,Province:"Manitoba"},{date:"11-06-2020",toll:3,Province:"Ontario"},{date:"07-06-2020",toll:89,Province:"Manitoba"},{date:"06-06-2020",toll:90,Province:"Ontario"},{date:"06-06-2020",toll:45,Province:"Nova Scotia"},{date:"13-06-2020",toll:55,Province:"Ontario"},{date:"13-06-2020",toll:1,Province:"Ontario"},{date:"13-06-2020",toll:17,Province:"Ontario"},{date:"12-06-2020",toll:2,Province:"Nova Scotia"},{date:"08-06-2020",toll:8,Province:"Ontario"},{date:"08-06-2020",toll:9,Province:"Newfoundland "},{date:"06-06-2020",toll:11,Province:"Newfoundland "},{date:"12-06-2020",toll:100,Province:"Ontario"},{date:"06-06-2020",toll:13,Province:"Ontario"}]; function datefromDDMMYYFormat(str) { return new Date(str.split('-').reverse().join('-')); } const startDate = datefromDDMMYYFormat("03-06-2020"); const endDate = datefromDDMMYYFormat("13-06-2020"); const betweenDate = data.filter(f => { const date = datefromDDMMYYFormat(f.date); return (date >= startDate && date <= endDate && f.Province == "Ontario"); }); console.log(betweenDate);

The problem of the poorly formed date string was solved in the accepted answer.

But this answer still has a problem: it uses a string as an argument to the Date() constructor. According to the MDN web docs , passing a string argument to Date() :

is strongly discouraged due to browser differences and inconsistencies.

Instead of initializing Date() with a string, use this form:

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])

This will prevent inconsistent results across browsers.

The following getTimeFromDateStr() function converts a date string of the form 'dd-mm-yyyy' to a timestamp, which can be compared easily:

 function getTimeFromDateStr(ddMmYyyy) { let [day, month, year] = ddMmYyyy.split('-'); return new Date(year, month-1, day, 12).getTime(); } let ts1 = getTimeFromDateStr('14-02-2020'); let ts2 = getTimeFromDateStr('15-02-2020'); console.log(`'15-02-2020' > '14-02-2020' => '`, ts2 > ts1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM