Created
June 15, 2010 14:49
-
-
Save dirceu-jr/439220 to your computer and use it in GitHub Desktop.
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
/* | |
* JavaScript Pretty Date | |
* Copyright (c) 2008 John Resig (jquery.com) | |
* Licensed under the MIT license. | |
*/ | |
// Takes an ISO time and returns a string representing how | |
// long ago the date represents. | |
TimeInWordsHelper = { | |
distanceInWords: function(time) { | |
var date = time, | |
diff = (((new Date()).getTime() - date.getTime()) / 1000), | |
day_diff = Math.floor(diff / 86400); | |
if ( isNaN(day_diff) || day_diff < 0) | |
return; | |
return day_diff == 0 && ( | |
diff < 60 && Math.floor( diff ) + " segundos" || | |
diff < 120 && "1 minuto" || | |
diff < 3600 && Math.floor( diff / 60 ) + " minutos" || | |
diff < 7200 && "1 hora" || | |
diff < 86400 && Math.floor( diff / 3600 ) + " horas") || | |
day_diff == 1 && "1 dia" || | |
day_diff < 7 && day_diff + " dias" || | |
day_diff < 14 && "1 semana" || | |
day_diff < 31 && Math.ceil( day_diff / 7 ) + " semanas" || | |
day_diff < 355 && Math.ceil( day_diff / 31 ) + " meses" || | |
day_diff > 356 && "Muito tempo"; | |
}, | |
convertBySelector: function(el, selector) { | |
var now = new Date() | |
// implementei um class selector simples para evitar bloat do jquery (codigo desnecessario) no widgets | |
var els = document.getElementsByTagName(el); | |
for (var i=0; i < els.length; i++) { | |
if (els[i].getAttribute('class') == selector) { | |
var oldInner = els[i].innerHTML; | |
var title = els[i].getAttribute('title'); | |
if (title.length > 10) { | |
var date = new Date(title); | |
} else { | |
var date = new Date(title*1000); | |
els[i].setAttribute('title', date); | |
} | |
els[i].innerHTML = TimeInWordsHelper.distanceInWords(date) + ' atrás' | |
} | |
} | |
} | |
}; | |
try { | |
TimeInWordsHelper.convertBySelector('span', 'gmttime'); | |
} catch(e){} | |
setInterval(function() { | |
TimeInWordsHelper.convertBySelector('span', 'gmttime'); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment