Created
February 2, 2012 00:28
-
-
Save kidpollo/1720383 to your computer and use it in GitHub Desktop.
proper syntax for cached dates
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 localizable_time_ago_in_words(from, locale){ | |
"use strict"; | |
return distance_of_time_in_words(new Date(), new Date(from)); | |
} | |
function distance_of_time_in_words(to, from) { | |
"use strict"; | |
var seconds_ago = ((to - from) / 1000); | |
var minutes_ago = Math.floor(seconds_ago / 60); | |
/*!<sl:translate>*/ | |
if(minutes_ago <= 0) { return "less than a minute ago";} | |
if(minutes_ago === 1) { return "a minute ago";} | |
if(minutes_ago < 45) { return "{0} minutes ago".format(minutes_ago);} | |
if(minutes_ago < 90) { return "1 hour ago";} | |
var hours_ago = Math.round(minutes_ago / 60); | |
if(minutes_ago < 1440) { return "{0} hours ago".format(hours_ago);} | |
if(minutes_ago < 2880) { return "1 day ago";} | |
var days_ago = Math.round(minutes_ago / 1440); | |
if(minutes_ago < 43200) { return "{0} days ago".format(days_ago);} | |
if(minutes_ago < 86400) { return "1 month ago";} | |
var months_ago = Math.round(minutes_ago / 43200); | |
if(minutes_ago < 525960) { return "{0} months ago".format(months_ago);} | |
if(minutes_ago < 1051920) { return "1 year ago";} | |
var years_ago = Math.round(minutes_ago / 525960); | |
return "over {0} years ago".format(years_ago); | |
/*!</sl:translate>*/ | |
} | |
if(typeof jQuery !== 'undefined') { | |
(function($) { | |
"use strict"; | |
function convert_to_relative_date(element){ | |
var timestamp = $(element).attr('timestamp'); | |
var locale = $(element).attr('locale'); | |
$(element).html( | |
localizable_time_ago_in_words(parseInt(timestamp, 10), locale) | |
); | |
} | |
function convert_all_dates_to_relative_date(){ | |
$('.i18n-date').each(function(){ | |
convert_to_relative_date(this); | |
}); | |
} | |
// apply quen document loads | |
$(document).ready(function(){ | |
convert_all_dates_to_relative_date(); | |
//apply whenever ajaxy stuff happens | |
$(document).ajaxComplete(function(){ | |
convert_all_dates_to_relative_date(); | |
}); | |
}); | |
})(jQuery); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment