Created
August 21, 2009 18:23
-
-
Save dcneiner/172279 to your computer and use it in GitHub Desktop.
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
// Adapted from code provided by 37Signals on their Signal vs. Noise blog. | |
// Added support for Twitter date parsing. | |
// Call DateHelper.time_ago_in_words_with_parsing(twitter_date_as_string) to use. | |
var DateHelper = { | |
date_parse_from_twitter: function(str){ | |
parts = str.match(/^([a-z]{3}) ([a-z]{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) \+(\d{4}) (\d{4})$/i); | |
if(!parts) return false; | |
var date_str = parts[1] + ", " + parts[3] + " " + parts[2] + " " + parts[8] + " " + parts[4] + ":" + parts[5] + ":" + parts[6] + " GMT"; | |
var date = new Date(date_str); | |
return date; | |
}, | |
time_ago_in_words_with_parsing: function(from) { | |
var date = this.date_parse_from_twitter(from); | |
if(!date) return ""; | |
return this.time_ago_in_words(date); | |
}, | |
time_ago_in_words: function(from) { | |
return this.distance_of_time_in_words(new Date, from); | |
}, | |
distance_of_time_in_words: function(to, from) { | |
var distance_in_seconds = ((to - from) / 1000); | |
var distance_in_minutes = Math.floor(distance_in_seconds / 60.0); | |
if (distance_in_minutes == 0) { return 'less than a minute ago'; }; | |
if (distance_in_minutes == 1) { return 'a minute ago'; }; | |
if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes ago'; }; | |
if (distance_in_minutes < 90) { return 'about 1 hour ago'; }; | |
if (distance_in_minutes < 1440) { return 'about ' + Math.floor(distance_in_minutes / 60) + ' hours ago'; }; | |
if (distance_in_minutes < 2880) { return '1 day ago'; }; | |
if (distance_in_minutes < 43200) { return Math.floor(distance_in_minutes / 1440) + ' days ago'; }; | |
if (distance_in_minutes < 86400) { return 'about 1 month ago'; }; | |
if (distance_in_minutes < 525960) { return Math.floor(distance_in_minutes / 43200) + ' months ago'; }; | |
if (distance_in_minutes < 1051199) { return 'about 1 year ago'; }; | |
return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years ago'; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment