Created
May 7, 2013 08:19
-
-
Save qborreda/5531049 to your computer and use it in GitHub Desktop.
Cálculo en texto del tiempo pasado desde un datetime dado
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
/** | |
* Calculates the Twitter time since the tweet was created | |
* @param datetime returned by Twitter API in created_at | |
* @return time since in html | |
*/ | |
function calculateSince(datetime) | |
{ | |
var tTime=new Date(datetime); | |
var cTime=new Date(); | |
var sinceMin=Math.round((cTime-tTime)/60000); | |
var since, sinceDay, sinceSec, sinceHr; | |
if(sinceMin===0) | |
{ | |
sinceSec=Math.round((cTime-tTime)/1000); | |
if(sinceSec<10) | |
since='less than 10 seconds ago'; | |
else if(sinceSec<20) | |
since='less than 20 seconds ago'; | |
else | |
since='half a minute ago'; | |
} | |
else if(sinceMin==1) | |
{ | |
sinceSec=Math.round((cTime-tTime)/1000); | |
if(sinceSec==30) | |
since='half a minute ago'; | |
else if(sinceSec<60) | |
since='less than a minute ago'; | |
else | |
since='1 minute ago'; | |
} | |
else if(sinceMin<45) | |
since=sinceMin+' minutes ago'; | |
else if(sinceMin>44&&sinceMin<60) | |
since='about 1 hour ago'; | |
else if(sinceMin<1440){ | |
sinceHr=Math.round(sinceMin/60); | |
if(sinceHr==1) | |
since='about 1 hour ago'; | |
else | |
since='about '+sinceHr+' hours ago'; | |
} | |
else if(sinceMin>1439&&sinceMin<2880) | |
since='1 day ago'; | |
else | |
{ | |
sinceDay=Math.round(sinceMin/1440); | |
since=sinceDay+' days ago'; | |
} | |
return since; | |
}; | |
//auto refresh interval to load more tweets | |
setInterval(function() | |
{ | |
console.log('updating time since...'); | |
var tweets = $('#tweets .tweet'); | |
$.each(tweets, function(i,v) | |
{ | |
//update the time since for the tweet | |
$(v).find('.tweet-time').html(calculateSince($(v).find('.tweet-user').attr('created_at'))).fadeIn(); | |
}); | |
}, 30000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment