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

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

    I write one with js and python, used in two projects, very nice and simple: a simple library (less then 2kb) used to format date with *** time ago statement.

    simple, small, easy used, and well tested.

    1. npm install timeago.js

    2. import timeago from 'timeago.js'; // or use script tag

    3. use api format.

    Sample:

    var timeagoIns  = timeago();
    timeagoIns .format('2016-06-12');
    

    Also you can render in real-time.

    var timeagoIns = timeago();
    timeagoIns.render(document.querySelectorAll('time'));
    

提交回复
热议问题