Created
November 29, 2010 19:11
-
-
Save caged/720395 to your computer and use it in GitHub Desktop.
All the parts we use to convert dates on the page to local times
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 date = new Date(); | |
var offset = date.getTimezoneOffset(); | |
$.cookie('tzoffset', offset, { path: '/' }) | |
/** | |
* Add age-based class name | |
*/ | |
$this.find('p.date .timestamp').each(function() { | |
var el = $(this), | |
date = $.parseUTCDate(el.attr('title')), | |
age = '', | |
diff = ((new Date()).getTime() - date.getTime()) / 1000 / 60 / 60; // convert to hours | |
if(diff >= 2 * 7 * 24) age = 'oldest'; // Two weeks | |
else if(diff >= 7 * 24) age = 'old'; // One week | |
else if(diff >= 3 * 24) age = 'stale'; // 3 days | |
else if (diff >= 12) age = 'fresh'; | |
else age = 'new'; | |
el.addClass('age-' + age); | |
}); | |
/** | |
* Convert any span.timestamp to the users local time and make it relative | |
* if required | |
*/ | |
$('span.timestamp').each(function() { | |
var $this = $(this); | |
if($this.hasClass('parsed')) return; | |
var utc = $.edate.parseUTC($this.text()), | |
rel = $this.attr('rel'); | |
if(rel == 'words') { | |
output = $.edate.timeAgoInWords(utc) | |
} else if(rel == 'words_old') { | |
output = $.edate.timeAgoInWords(utc) | |
output.replace('ago', 'old') | |
} else { | |
output = $.edate.strftime(utc, rel) | |
} | |
$this.text(output).addClass('parsed') | |
}) | |
/** | |
* Parse a date as UTC | |
*/ | |
$.parseUTCDate = function(value) { | |
var localDate = new Date(value); | |
var utcSeconds = Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(), localDate.getHours(), localDate.getMinutes(), localDate.getSeconds()); | |
return new Date(utcSeconds); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment