[Vuejs]-In vue, how do I filter a data object to only show values from the last 24 hours?

0👍

In Moment, you can check if a date is within the last 24 hours with:

moment().diff(yourDate, 'hours') < 24

(note that future dates will also pass this check, but you can easily adjust it).

You can put this into your computed property:

newTasks(){
  if(!this.tasks){
    return []
  }
  return this.tasks.filter(task => !task.done && moment().diff(task.creationDateTime, 'hours') < 24)
}

And that’s it, now newTasks should contain all tasks from the last 24 hours that are not done.

Leave a comment