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

后端 未结 25 1909
遥遥无期
遥遥无期 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
    

提交回复
热议问题