Skip to content

Instantly share code, notes, and snippets.

@radelmann
Created October 26, 2015 22:25
Show Gist options
  • Select an option

  • Save radelmann/87079dc1f9f9eec35511 to your computer and use it in GitHub Desktop.

Select an option

Save radelmann/87079dc1f9f9eec35511 to your computer and use it in GitHub Desktop.

Revisions

  1. radelmann created this gist Oct 26, 2015.
    19 changes: 19 additions & 0 deletions getTimeDiff.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    var getTimeDiff = function(date) {
    //input: js date object
    //output: returns how long ago the input date occured in a user friendly format
    var now = new Date().getTime();
    var ms = (now - date.getTime());
    var days = Math.round(ms / 86400000); // days
    var hrs = Math.round((ms % 86400000) / 3600000); // hours
    var mins = Math.round(((ms % 86400000) % 3600000) / 60000); // minutes

    if (days > 0) {
    return days + ' days ago'
    } else if (hrs > 0) {
    return hours + ' hours ago'
    } else if (mins > 0) {
    return mins + ' mins ago'
    } else {
    return 'just now'
    }
    }