1

I have an array of data similar to this:

var items  = [
  { id: 84, "completedDate":"2019-01-26T17:45:07.895Z" },
  { id: 92, "completedDate":"2019-02-26T17:45:07.895Z" }, 
  { id: 123, "completedDate":"2019-03-26T17:45:07.895Z" }, 
  { id: 2353, "completedDate":"2019-04-26T17:45:07.895Z" }
];

I would like to return an array with only objects less than 30 days old.

I have tried to filter

var filtered = items.filter(function(item) { 
  return moment(item.completedDate) > moment.subtract(30, 'days');  
});

Is this what I need to do, or is there a better way to do this?

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Phil Lachmann
  • 211
  • 4
  • 14
  • Does it work as expected? – The Head Rush May 07 '19 at 17:13
  • 1
    Possible duplicate of [Moment js date time comparison](https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison) –  May 07 '19 at 17:13
  • 1
    Do you need moment? it could probably be done without it if you aren't needing that library for anything else. – Chris Barr May 07 '19 at 17:13
  • JSON is JavaScript Object Notation, and is a string representation of JavaScript Objects. Where where does the string representation come into this? (i.e. you should not have the `JSON` tag nor reference JSON) – crashmstr May 07 '19 at 17:15

3 Answers3

1

You don't need moment to compare dates:

const compareDate = new Date();
compareDate.setDate(compareDate.getDate() - 30);

const filtered = items.filter(item => new Date(item.completedDate) > compareDate);
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
1

Here's a similar way to do this without moment. here we just get the current day, reset the time back to the start of the day (you may or may not need this for your use case) and then we just use plain JS date objects to compare

var items  = [
  { id: 84, "completedDate":"2019-01-26T17:45:07.895Z" },
  { id: 92, "completedDate":"2019-02-26T17:45:07.895Z" }, 
  { id: 123, "completedDate":"2019-03-26T17:45:07.895Z" }, 
  { id: 2353, "completedDate":"2019-04-26T17:45:07.895Z" }
];

var thirtyDaysAgo = new Date();
thirtyDaysAgo.setHours(0, 0, 0, 0);
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);

var filtered = items.filter(function(item) { 
  var d = new Date(item.completedDate).getTime();
  return d > thirtyDaysAgo;  
});

console.log(filtered);

Or, an even smaller filter function (if you don't need IE 11 support) would be:

var filtered = items.filter((item) => new Date(item.completedDate).getTime() > thirtyDaysAgo);
Chris Barr
  • 29,851
  • 23
  • 95
  • 135
0

try

items.filter( x=> x.completedDate > today.toISOString() );

var items  = [
  { id: 84, "completedDate":"2019-01-26T17:45:07.895Z" },
  { id: 92, "completedDate":"2019-02-26T17:45:07.895Z" }, 
  { id: 123, "completedDate":"2019-03-26T17:45:07.895Z" }, 
  { id: 2353, "completedDate":"2019-04-26T17:45:07.895Z" }
];

var today = new Date("2019-04-20T17:45:07.895Z") // or: new Date() 
today = new Date(+today - 30 *86400000)

let r= items.filter( x=> x.completedDate > today.toISOString() );

console.log(r);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345