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

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

     I achieve this by following method
    
       timeAgo = (date) => {
                var ms = (new Date()).getTime() - date.getTime();
                var seconds = Math.floor(ms / 1000);
                var minutes = Math.floor(seconds / 60);
            var hours = Math.floor(minutes / 60);
            var days = Math.floor(hours / 24);
            var months = Math.floor(days / 30);
            var years = Math.floor(months / 12);
        
            if (ms === 0) {
                return 'Just now';
            } if (seconds < 60) {
                return seconds + ' seconds Ago';
            } if (minutes < 60) {
                return minutes + ' minutes Ago';
            } if (hours < 24) {
                return hours + ' hours Ago';
            } if (days < 30) {
                return days + ' days Ago';
            } if (months < 12) {
                return months + ' months Ago';
            } else {
                return years + ' years Ago';
            }
        
        }
        
            console.log(timeAgo(new Date()));
            console.log(timeAgo(new Date('Jun 27 2020 10:12:19')));
            console.log(timeAgo(new Date('Jun 27 2020 00:12:19')));
            console.log(timeAgo(new Date('May 28 2020 13:12:19')));
            console.log(timeAgo(new Date('May 28 2017 13:12:19')));
    

提交回复
热议问题