<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ralph Holzmann &#187; Relative</title>
	<atom:link href="http://ralphholzmann.com/tag/relative/feed/" rel="self" type="application/rss+xml" />
	<link>http://ralphholzmann.com</link>
	<description>Web Development - jQuery, PHP, MySQL. In that order.</description>
	<lastBuildDate>Fri, 29 Jan 2010 14:01:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Relative Date String in JavaScript</title>
		<link>http://ralphholzmann.com/2009/12/14/relative-date-string-in-javascript/</link>
		<comments>http://ralphholzmann.com/2009/12/14/relative-date-string-in-javascript/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 17:32:06 +0000</pubDate>
		<dc:creator>Ralph</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Relative]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Time]]></category>

		<guid isPermaLink="false">http://ralphholzmann.com/?p=32</guid>
		<description><![CDATA[Here&#8217;s a script I wrote awhile ago that&#8217;s used in this site and a few other projects of mine. It&#8217;s a method attached to the JavaScript Date object to output the date relative to now. It works for both past &#8230; <a href="http://ralphholzmann.com/2009/12/14/relative-date-string-in-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a script I wrote awhile ago that&#8217;s used in this site and a few other projects of mine. It&#8217;s a method attached to the JavaScript Date object to output the date relative to now. It works for both past and future dates.</p>
<pre class="brush:js" jsbin="http://jsbin.com/ugupi">Date.prototype.toRelativeString = function() {

  // Calculate difference between date and now
  var today = parseInt((new Date()).getTime() / 1000);
  var date = parseInt(this.getTime() / 1000);
  var second_difference = today - date;
  var day_difference = Math.floor(second_difference / 86400);
  var when_text, one_off;

  // Determine if date is in future or past
  if (second_difference &lt; 0) {
    when_text = ' from now';
    one_off = 'Tomorrow';
    second_difference = -second_difference;
    day_difference = -day_difference
  } else {
    one_off = 'Yesterday';
    when_text = ' ago';
  }

  // Return relative string
  if(day_difference == 0) {
      if(second_difference &lt; 60) {
          return 'Just now';
      } else if(second_difference &lt; 120) {
          return '1 minute' + when_text;
      } else if(second_difference &lt; 3600) {
          return Math.floor(second_difference/60) + ' minutes' + when_text;
      } else if(second_difference &lt; 7200) {
          return '1 hour' + when_text
      } else if(second_difference &lt; 86400) {
          return Math.floor(second_difference/3600) + ' hours' + when_text;
      }
  } else if(day_difference == 1) {
      return one_off;
  } else if(day_difference &lt; 7) {
      return day_difference + ' days' + when_text;
  } else if(day_difference == 7) {
      return '1 week' + when_text;
  } else if(day_difference &lt; (7*6)) {
      return Math.ceil(day_difference/7) + ' weeks' + when_text;
  } else if(day_difference &lt; 365) {
      return Math.ceil(day_difference/(365/12)) + ' months' + when_text
  } else {
      years = Math.round(day_difference/365);
      return years + ' year' + (years != 1 ? 's' : '') + when_text;
  }
};</pre>
<p>Here&#8217;s some examples:</p>
<pre class="brush:js" jsbin="http://jsbin.com/ugupi">var now = new Date();
document.write(now.toRelativeString() + '&lt;br /&gt;'); // Just now

now.setDate(parseInt(now.getDate()) + 3);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // 3 days from now

now.setDate(parseInt(now.getDate()) - 4);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // Yesterday

now.setDate(parseInt(now.getDate()) - 4);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // 5 days ago

now.setDate(parseInt(now.getDate()) - 7);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // 2 weeks ago

now.setFullYear(parseInt(now.getFullYear()) - 7);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // 7 years ago

now.setFullYear(parseInt(now.getFullYear()) + 10);
document.write(now.toRelativeString() + '&lt;br /&gt;'); // 3 years from now</pre>
<p>It comes in very handy when dealing with dates. Hope you enjoy and please let me know if there are any bugs.</p>
]]></content:encoded>
			<wfw:commentRss>http://ralphholzmann.com/2009/12/14/relative-date-string-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
