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();
5
  • 1
    "dd-MM-yyyy" is not a valid date string in JavaScript. Commented Jun 14, 2020 at 14:36
  • new Date("13-06-2020") is invalid since there is no 13th month. Commented Jun 14, 2020 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 Commented Jun 14, 2020 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. Commented Jun 14, 2020 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})) Commented Jun 14, 2020 at 14:41

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the method and the explanation
new Date(str.split('-').reverse().join('-')) - phew, that is disgusting ... and impressive :D
@luk2302 Guilty, I love chaining split and join :D
@blex Passing a string argument to the Date() constructor is strongly discouraged. See my answer for details.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.