How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites

后端 未结 25 1594
遥遥无期
遥遥无期 2020-11-22 12:57

The question is how to format a JavaScript Date as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.

e.g.<

相关标签:
25条回答
  • 2020-11-22 13:34

    Can also use the dayjs relativeTime plugin to solve this.

    import * as dayjs from 'dayjs';
    import * as relativeTime from 'dayjs/plugin/relativeTime';
    
    dayjs.extend(relativeTime);
    dayjs(dayjs('1990')).fromNow(); // x years ago
    
    0 讨论(0)
提交回复
热议问题