Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Williammer/ffcc26ead1a9732126f0 to your computer and use it in GitHub Desktop.
Save Williammer/ffcc26ead1a9732126f0 to your computer and use it in GitHub Desktop.
javascript.tryCatchTest.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta property="wb:webmaster" content="edcf77ed05a8765f" />
<meta name="viewport" content="width=device-width">
<script>
var ErrorOverLimit = new Error('>= 31.');
var ErrorMsg = new Error('invalid time.');
function prettyDate(time){
var date = new Date(time || ""),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if(day_diff >= 31) {
throw ErrorOverLimit;
return;
} else if ( isNaN(day_diff) || day_diff < 0 ){
throw ErrorMsg;
return;
}
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) +
" minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) +
" hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) +
" weeks ago";
}
window.onload = function() {
var links = document.getElementsByTagName("a");
for ( var i = 0; i < links.length; i++ ) {
if ( links[i].title ) {
try {
var date = prettyDate(links[i].title);
} catch(e){
console.log('Error:'+e.message);
}
if ( date ) {
links[i].innerHTML = date;
}
}
}
};
</script>
</head>
<body>
<ul>
<li class="entry">
<p>blah blah blah...</p>
<small class="extra">
Posted <span class="time">
<a href="/2008/01/blah/57/" title="2008-01-28T20:24:17Z">
<span>January 28th, 2008</span>
</a>
</span>
by <span class="author"><a href="/john/">John Resig</a></span>
</small>
</li>
<!-- more list items -->
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment