Skip to content

Instantly share code, notes, and snippets.

@appden
Created April 29, 2009 16:54
Show Gist options
  • Save appden/103895 to your computer and use it in GitHub Desktop.
Save appden/103895 to your computer and use it in GitHub Desktop.
// Auto-updating Date stuff
Date.pretty = new Class({
Implements: Options,
options: {
interval: 60000,
threshold: '24 hours'
},
initialize: function(elements, options){
this.setOptions(options);
this.elements = [];
this.dates = [];
var bits = this.options.threshold.match(/(\d+)\s*(second|minute|hour|day|week|month|year)?/i);
this.threshold = (!bits) ? 0 : new Date().decrement( (bits[2] || 'day').toLowerCase(), +bits[1] );
return this.include(elements);
},
update: function(){
this.elements.each(function(element, i){
element.set('text', this.dates[i].timeDiffInWords());
}, this);
this.timer = this.update.delay(this.options.interval, this);
return this;
},
include: function(elements){
$$(elements).each(function(element){
if (this.elements.contains(element)) return;
var date = Date.parse( element.get('text').clean() );
if (date.isValid() && date > this.threshold){
this.dates.push(data);
this.elements.push(element);
}
}, this);
return this.update();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment