Use moment to check if date is from 2 minutes ago

后端 未结 1 362
轻奢々
轻奢々 2021-01-18 08:46

I tried this but I can\'t make it work.

I have an array of dates like this: [\'5/15/2017, 2:59:06 PM\', \'5/15/2017, 2:59:16 PM\', ...]

And I wa

相关标签:
1条回答
  • 2021-01-18 09:41

    Part of the issue is that you're only looking at the hour of the day, but you're actually comparing absolute dates.

    The other issue you have is moment isn't correctly parsing your date strings. To rectify this, you can provide a format string to instruct moment on how to parse apart the important bits:

    const PARSE_FORMAT = 'M/D/YYYY, H:mm:ss A';
    const twoMinutesAgo = moment().subtract(2, 'minutes')
    myArray.filter(stat => moment(stat.date, PARSE_FORMAT).isAfter(twoMinutesAgo));
    
    0 讨论(0)
提交回复
热议问题