<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users116</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/116" rel="self"/>
  <title>Tomasz `neo` Ko&#195;&#8230;&#226;&#8364;&#353;odziejski</title>
  <updated>Wed Oct 03 06:20:35 -0700 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor241</id>
    <published>2007-10-03T06:20:35-07:00</published>
    <title>[JavaScript] On Beautify JS Date to how recently the event occured.</title>
    <content type="html">&lt;p&gt;I think that using the getTime function isn't necessary. Separating these two arrays would be much better I think and easier to modifying. In the 8 line we can use ~~. &lt;/p&gt;

&lt;pre&gt;Date.prototype.when = function(){
	var diff = (new Date() - this)/1e3,
	u = [ &amp;quot;second&amp;quot;, &amp;quot;minute&amp;quot;, &amp;quot;hour&amp;quot;, &amp;quot;day&amp;quot;, &amp;quot;weeks&amp;quot;, &amp;quot;month&amp;quot;, &amp;quot;year&amp;quot; ],
        s = [ 1, 60, 60, 24, 7, 4.333, 12, 1e9], // again fast hack - 1e9
        i=0;
	
	for(;;i++){
		if((diff/=s[i])&amp;lt;1){
			return ~~(diff*=s[i])+' '+u[i-1]+(diff&amp;gt;1?'s':0);
		}
	}
	
}&lt;/pre&gt;</content>
    <author>
      <name>Tomasz `neo` Ko&#195;&#8230;&#226;&#8364;&#353;odziejski</name>
      <email></email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/37-beautify-js-date-to-how-recently-the-event-occured/refactors/241" rel="alternate"/>
  </entry>
</feed>

