3

I am making an API call that returns a large set of data. From that data I want to filter out and get only the records with a due date greater then or equal to today and less then or equal to 30 days from now.

But my filter is returning an incorrect set of records.

addDays = (date, days) => {
        var result = new Date(date);
        result.setDate(result.getDate() + days);
        return result;
    }

let today = new Intl.DateTimeFormat('en-US').format(new Date()); //returns "07/31/2020" as expected
let add30 = new Intl.DateTimeFormat('en-US').format(this.addDays(today, 30)); //returns "08/30/2020 as expected

let dueInThirtyDays = allActivities.filter(activity => new Intl.DateTimeFormat('en-US').format(new Date(activity.dueDate)) >= today && new Intl.DateTimeFormat('en-US').format(new Date(activity.dueDate)) <= add30 && new Intl.DateTimeFormat('en-US').format(new Date(activity.closedDate) === null));


//where format of activity.dueDate = "2020-01-14" which is whay its wrapped in the DateTimeFormat method

EXAMPLE of the array of Objects I am filtering through:

{
typeCode: "BCA "
categoryCode: "PN "
activityDescription: "PNACT03 TIER2 PRIMARY"
effectiveDate: "2010-10-14"
statusDate(pin): null
closedDate(pin): "2012-06-30"
dueDate(pin): "2011-01-14"
}

I am noticing that the format string return a date that is one day before the date given but I'm getting results back that are months ad years before today and well after 30 days from today. Its also return results where closedDate is not null

3 Answers 3

4

Please try the following solution

  const data = [
    {
      typeCode: "BCA ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2011-01-14",
        },
    {
      typeCode: "VGA ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2011-01-14",
        },
    {
      typeCode: "ABC ",
      categoryCode: "PN ",
      activityDescription: "PNACT03 TIER2 PRIMARY",
      effectiveDate: "2010-10-14",
      'statusDate(pin)': null,
      'closedDate(pin)': "2012-06-30",
      'dueDate(pin)': "2020-08-14",
        }
  ]
  const from = Date.now()
  const until = new Date().setDate(new Date().getDate() + 30)
  
  const output = data.filter(entry => {
    const time = new Date(entry['dueDate(pin)']).getTime()
    
    return time >= from && time <= until
  })
  
  console.log(output)
  

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

4 Comments

if you would please add some explanation to OP what, how, and why you do it (like this), then your answer would worth even more!
This example return 0 results for me when I should be getting 25 records back
Yor are right muka, basically I am suggesting to convert your dates strings to timestamp and the compare as numbers
Could you share some sample data and the expected output? Thanks
1

Your problem lies within the date-format you use to do the comparison.

Lets take for example this two dates:

  1. "10/31/2020"
  2. "11/30/2019"

A human can see that the date in 2020 is not smaller than the date in 2019, but for the if-comparison this:

if ("10/31/2020" <= "11/30/2019")

is the same as writing:

if ("10312020" <= "11302019")

You can try this in your browser console by typing

console.log("10/31/2020" <= "11/30/2019");

You should probably use a unix timestamp for date-comparison or use the y/m/d Format if you want to do a comparison with strings.

Comments

1

OK - I wound up using a combination of both @Mario and @Christoph Kerns answers. My final solution look like this:

const fromUnix = new Date().getTime() / 1000;
const untilUnix = new Date().setDate(new Date().getDate() + 30)/ 1000;
let dueInThirtyDays = this.props.activities.allActivities.filter(activity =>(new Date(activity.dueDate).getTime() / 1000) >= fromUnix &&  (new Date(activity.dueDate).getTime() / 1000) <= untilUnix);

I ultimately decided to mark @Christooph as the right answer because (1) his provided a though explanation and (2) I think the conversion to UNIX time was what really did the trick. @Mario did provide some semantics that assisted in arriving to the correct answer as well. Thank you.

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.