Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Learning JS
Type Safety
Euler Problem 2 solution
Popular
Parsing of XML data has high CPU usage
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Breadth first cartesian product iterator
php refactoring
first BST
Learning JS
Too many if statements
clean the code
Pastable version of
Beautify JS Date to how recently the event occured.
<pre class='prettyprint' language='javascript'>/*** * Beautify date to how recent the event occurred compared to now. * Some examples: 1 second, 4 hours, 10 days, 1 year. * Example: * var oneFiftyMin = new Date(new Date().getTime() - 60000 * 150); * alert(oneFiftyMin.when()); // will display "2 hours" * * Handy for things like "Item posted " + someDate.when() + " ago." */ Date.prototype.when = function() { var diff = new Date().getTime() - this.getTime(); var when; // our return value //TODO: what if the time is in the future? //if (diff < 0) throw new Error ("Date is in future, check timezone?"; //one or more of these will be non-zero, but we only care about the biggest one (in scale of time) var secondsOld = Math.floor(diff / 1000); var minutesOld = Math.floor(diff / 60000); var hoursOld = Math.floor(diff / 3600000); var daysOld = Math.floor(diff / 86400000); var monthsOld = Math.floor(diff / 2592000000); var yearsOld = Math.floor(diff / (2592000000 * 12)); //your content is old! //determine which value is non-zero and assign appropriate text if (yearsOld > 0) { when = yearsOld + " year"; } else if (monthsOld > 0) { when = monthsOld + " month"; } else if (daysOld > 0) { when = daysOld + " day"; } else if (hoursOld > 0) { when = hoursOld + " hour"; } else if (minutesOld > 0) { when = minutesOld + " minute"; } else { when = Math.round(diff /1000) + " second"; } //add plural if necessary return (0 == when.indexOf("1 ")) ? when : when + "s"; }</pre> <a href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>