Created
October 26, 2015 22:25
-
-
Save radelmann/87079dc1f9f9eec35511 to your computer and use it in GitHub Desktop.
return how long ago a date occured in days, hours, or mins.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment