-
-
Save endel/7901929 to your computer and use it in GitHub Desktop.
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 distance_of_time_in_words, time_ago_in_words; | |
time_ago_in_words = function(from_time, include_seconds) { | |
if (include_seconds != null) { | |
include_seconds; | |
} else { | |
include_seconds = false; | |
}; | |
return distance_of_time_in_words(from_time, Date.now(), include_seconds); | |
}; | |
distance_of_time_in_words = function(from_time, to_time, include_seconds) { | |
var distance_in_minutes, distance_in_seconds, distance_in_years, minute_offset_for_leap_year, remainder; | |
if (include_seconds != null) { | |
include_seconds; | |
} else { | |
include_seconds = false; | |
}; | |
if (to_time != null) { | |
to_time; | |
} else { | |
to_time = 0; | |
}; | |
distance_in_minutes = Math.round(Math.abs(to_time - from_time) / 60 / 1000); | |
distance_in_seconds = Math.round(Math.abs(to_time - from_time) / 1000); | |
if (0 <= distance_in_minutes && distance_in_minutes <= 1) { | |
if (!include_seconds) { | |
if (distance_in_minutes === 0) { | |
return "less than 1 minute ago"; | |
} else { | |
return "" + distance_in_minutes + " minutes ago"; | |
} | |
} | |
if (0 <= distance_in_seconds && distance_in_seconds <= 4) { | |
return "less than 5 seconds ago"; | |
} else if (5 <= distance_in_seconds && distance_in_seconds <= 9) { | |
return "less than 10 seconds ago"; | |
} else if (10 <= distance_in_seconds && distance_in_seconds <= 19) { | |
return "less than 20 seconds ago"; | |
} else if (20 <= distance_in_seconds && distance_in_seconds <= 39) { | |
return "less than half a minute ago"; | |
} else if (40 <= distance_in_seconds && distance_in_seconds <= 59) { | |
return "less than 1 minute ago"; | |
} else { | |
return "1 minute ago"; | |
} | |
} else if (2 <= distance_in_minutes && distance_in_minutes <= 44) { | |
return "" + distance_in_minutes + " minutes ago"; | |
} else if (45 <= distance_in_minutes && distance_in_minutes <= 89) { | |
return "about 1 hour ago"; | |
} else if (90 <= distance_in_minutes && distance_in_minutes <= 1439) { | |
return "about " + (Math.round(distance_in_minutes / 60.0)) + " hours ago"; | |
} else if (1440 <= distance_in_minutes && distance_in_minutes <= 2529) { | |
return "1 day ago"; | |
} else if (2530 <= distance_in_minutes && distance_in_minutes <= 43199) { | |
return "" + (Math.round(distance_in_minutes / 1440.0)) + " days ago"; | |
} else if (43200 <= distance_in_minutes && distance_in_minutes <= 86399) { | |
return "about 1 month ago"; | |
} else if (86400 <= distance_in_minutes && distance_in_minutes <= 525599) { | |
return "" + (Math.round(distance_in_minutes / 43200.0)) + " months ago"; | |
} else { | |
distance_in_years = distance_in_minutes / 525600; | |
minute_offset_for_leap_year = (distance_in_years / 4) * 1440; | |
remainder = (distance_in_minutes - minute_offset_for_leap_year) % 525600; | |
if (remainder < 131400) { | |
return "about " + (Math.round(distance_in_years)) + " years ago"; | |
} else if (remainder < 394200) { | |
return "over " + (Math.round(distance_in_years)) + " years ago"; | |
} else { | |
return "almost " + (Math.round(distance_in_years + 1)) + " years ago"; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment