Remove JSON array items whose CreatedDate is not within last 5 minutes

I have a function ("get only the recent") to filter out JSON items from JSON array where CreatedDate was not created within the last 5 minutes.

OR to put it in another perspective.......

keep all JSON array items that have a CreatedDate within the last 5 minutes.

I am just missing something and I can't seem to see it.

var now  = new Date()
console.info("Now:                " + now);
var fiveMinutesAgo  = new Date()
fiveMinutesAgo.setMinutes(-5)

console.info("Filter by Date:     " + fiveMinutesAgo);

var permitDateTime = new Date(el => el.DateCreated);

msg.payload = msg.payload.filter(el => el.DateCreated.includes(permitDateTime.getTime() < fiveMinutesAgo.getTime()))


return msg;

Put some debugging in using node.warn(). Like
node.warn("permitDateTime"+permitDateTime)
node.warn(el.DateCreated.includes(permitDateTime.getTime())
to get an idea of what the data actually is.

hmm, a few issues.

try this...

var now  = new Date()
var fiveMinutesAgo  = now.getTime() - (5*60000); //epoch minus 5 mins
msg.origPayload = msg.payload; //keep old one for reference / debugging
msg.fiveMinutesAgo = fiveMinutesAgo; //add ts it to msg so we can inspect it in a debug set to show complete message
msg.payload = msg.payload.filter(el =>  new Date(el.DateCreated).getTime() >= fiveMinutesAgo )

return msg;

EDIT...
let me explain this line msg.payload = msg.payload.filter(el => new Date(el.DateCreated).getTime() >= fiveMinutesAgo )

I have no idea what the type of el.DateCreated is. It could be a string, it could be a date object - BUT - to get you going, I am taking no chances, so, I create a new Date from whatever it is, then get its epoch, then compare it as a number to the fiveMinutesAgo variable.

note also, I add a few other items to the msg object - this is so I can view / debug the whole operation in the debug sidebar (set your debug to show complete message)

1 Like

I will say one more thing - where are you getting this data? from a database? If so, you really should be doing this at the database side with a WHERE clause.

Thank you! So very much!

This data was retrieved from Wufoo, using the Wufoo http get API.
But, I agree. Using a SQL WHERE clause would be much easier.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.