Skip to content

Instantly share code, notes, and snippets.

@davidreuss
Forked from rails/gist:58761
Created February 9, 2009 10:39
Show Gist options
  • Save davidreuss/60739 to your computer and use it in GitHub Desktop.
Save davidreuss/60739 to your computer and use it in GitHub Desktop.
var DateHelper = {
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words_with_parsing: function(from) {
var date = new Date;
date.setTime(Date.parse(from));
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 = (distance_in_seconds / 60).floor();
if (distance_in_minutes == 0) { return 'under 1 minut siden'; }
if (distance_in_minutes == 1) { return '1 minut siden'; }
if (distance_in_minutes < 45) { return distance_in_minutes + ' minutter siden'; }
if (distance_in_minutes < 90) { return 'omkring 1 time siden'; }
if (distance_in_minutes < 1440) { return 'about ' + (distance_in_minutes / 60).floor() + ' timer siden'; }
if (distance_in_minutes < 2880) { return '1 dag siden'; }
if (distance_in_minutes < 43200) { return (distance_in_minutes / 1440).floor() + ' dage siden'; }
if (distance_in_minutes < 86400) { return 'omkring 1 måned siden'; }
if (distance_in_minutes < 525960) { return (distance_in_minutes / 43200).floor() + ' måneder siden'; }
if (distance_in_minutes < 1051199) { return 'omkring 1 år siden'; }
return 'over ' + (distance_in_minutes / 525960).floor() + ' år siden';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment