Moment.js - tomorrow, today and yesterday

后端 未结 12 2094
悲哀的现实
悲哀的现实 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:09

    You can use .add() and .subtract() method to get yesterday and tomorrow date. Then use format method to get only date .format("D/M/Y"), D stand for Day, M for Month, Y for Year. Check in Moment Docs

     let currentMilli = Date.now()
     let today = Moment(currentMilli).format("D/M/Y");
     let tomorrow = Moment(currentMilli).add(1, 'days').format("D/M/Y");
     let yesterday = Moment(currentMilli).subtract(1, 'days').format("D/M/Y");
    

    Result will be:

    Current Milli - 1576693800000
    today - 19/12/2019
    tomorrow - 18/12/2019
    yesterday - 18/12/2019
    

提交回复
热议问题