0

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();
Chris
  • 2,953
  • 10
  • 48
  • 118
  • 1
    "dd-MM-yyyy" is not a [valid date string in JavaScript](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript). – str Jun 14 '20 at 14:36
  • `new Date("13-06-2020")` is invalid since there is no 13th month. – luk2302 Jun 14 '20 at 14:37
  • @str, its just how the data is being returned and I only mention the format so people clearly understood what the dates were – Chris Jun 14 '20 at 14:38
  • 1
    Yeah, but that not change the fact that js does not handle it well / at all. You have to write custom date parsing logic since your date format is simply not supported. – luk2302 Jun 14 '20 at 14:38
  • You can first get your `date` data in shape then you can start filtering: `data.map(({date, ...rest})=>({date:date.replace(/(\d+)-(\d+)/, '$2-$1'), ...rest}))` – gorak Jun 14 '20 at 14:41

2 Answers2

3

"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);
blex
  • 24,941
  • 5
  • 39
  • 72
0

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);
terrymorse
  • 6,771
  • 1
  • 21
  • 27