Mi Lugarcito

Node.js - Twitter Clone Coding // Styling the posts and Outputting posts onto the newsfeed 본문

Express.js

Node.js - Twitter Clone Coding // Styling the posts and Outputting posts onto the newsfeed

selene park 2021. 3. 31. 15:26

fontawesome.com/icons?d=gallery&p=2&q=speech%20bubble

 

Font Awesome

The world’s most popular and easiest to use icon set just got an upgrade. More icons. More styles. More Options.

fontawesome.com

TIP

 

 

 

 

javascript date to timestamp ago

 

stackoverflow.com/questions/6108819/javascript-timestamp-to-relative-time

 

Javascript timestamp to relative time

I'm looking for a nice JS snippet to convert a timestamp (e.g. from Twitter API) to a nice user friendly relative time (e.g. 2 seconds ago, one week ago etc). Anyone care to share some of their

stackoverflow.com

function timeDifference(current, previous) {

    var msPerMinute = 60 * 1000;
    var msPerHour = msPerMinute * 60;
    var msPerDay = msPerHour * 24;
    var msPerMonth = msPerDay * 30;
    var msPerYear = msPerDay * 365;

    var elapsed = current - previous;

    if (elapsed < msPerMinute) {
         return Math.round(elapsed/1000) + ' seconds ago';   
    }

    else if (elapsed < msPerHour) {
         return Math.round(elapsed/msPerMinute) + ' minutes ago';   
    }

    else if (elapsed < msPerDay ) {
         return Math.round(elapsed/msPerHour ) + ' hours ago';   
    }

    else if (elapsed < msPerMonth) {
        return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago';   
    }

    else if (elapsed < msPerYear) {
        return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago';   
    }

    else {
        return 'approximately ' + Math.round(elapsed/msPerYear ) + ' years ago';   
    }
}