Moment.js - tomorrow, today and yesterday

后端 未结 12 2089
悲哀的现实
悲哀的现实 2021-01-30 00:21

I\'d like the moment().fromNow() functionality, but when the date is close it is too precise - ex. I don\'t want it to show \'in 3 hours\' but \'today\' - so basica

12条回答
  •  [愿得一人]
    2021-01-30 01:02

    I use a combination of add() and endOf() with moment

    //...
    const today = moment().endOf('day')
    const tomorrow = moment().add(1, 'day').endOf('day')
    
    if (date < today) return 'today'
    if (date < tomorrow) return 'tomorrow'
    return 'later'
    //...
    

提交回复
热议问题