Created
December 20, 2011 06:54
-
-
Save Japh/1500589 to your computer and use it in GitHub Desktop.
Javascript snippet for plain english relative time (e.g. "X seconds ago")
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
| function time_ago(time) { | |
| periods = new Array("second", "minute", "hour", "day", "week", "month", "year", "decade"); | |
| lengths = new Array("60","60","24","7","4.35","12","10"); | |
| nowdate = new Date(); | |
| now = (nowdate.getTime() / 1000); | |
| difference = now - time; | |
| tense = "ago"; | |
| for(j = 0; difference >= lengths[j] && j < lengths.length-1; j++) { | |
| difference /= lengths[j]; | |
| } | |
| difference = Math.round(difference); | |
| if(difference != 1) { | |
| periods[j] += "s"; | |
| } | |
| return difference + ' ' + periods[j] + ' ago'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment