Created
March 4, 2011 20:24
-
-
Save kgn/855645 to your computer and use it in GitHub Desktop.
Nice date delta string: 3 minutes ago, 1 hour ago, 16 days ago
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
// modified from: http://ejohn.org/blog/javascript-pretty-date/ | |
(function($){ | |
$.prettyDate = function(time){ | |
var date = new Date((time || '')); | |
var diff = (((new Date()).getTime() - date.getTime())/1000); | |
var day_diff = Math.floor(diff / 86400); | |
if(diff === 0 || isNaN(day_diff) || day_diff < 0){ | |
return; | |
} | |
//display the hours up to 48, then start counting days after 2 | |
//days is the final interval | |
return ( | |
diff < 1 && '1 second ago' || | |
diff < 60 && Math.floor(diff) + ' seconds ago' || | |
diff < 120 && '1 minute ago' || | |
diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' || | |
diff < 7200 && '1 hour ago' || | |
diff < 172800 && Math.floor( diff / 3600 ) + ' hours ago' | |
) || day_diff >= 2 && ( | |
day_diff + ' days ago' | |
); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment