Created
June 2, 2015 01:03
-
-
Save jbnv/2ddb112d15a906ef3af5 to your computer and use it in GitHub Desktop.
ago(date) function to return a brief "ago" string.
This file contains 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 second = 1e3; | |
var minute = 6e4; | |
var hour = 36e5; | |
var day = 864e5; | |
var week = 6048e5; | |
function ago(diff) { | |
var unit, num; | |
if (diff <= second) { | |
unit = 's'; | |
num = 1; | |
} else if (diff < minute) { | |
unit = 's'; | |
num = diff / second; | |
} else if (diff < hour) { | |
unit = 'm'; | |
num = diff / minute; | |
} else if (diff < day) { | |
unit = 'h'; | |
num = diff / hour; | |
} else if (diff < week) { | |
unit = 'd'; | |
num = diff / day; | |
} else { | |
unit = 'w'; | |
num = diff / week; | |
} | |
return num + unit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment