<?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>Stephen Braitsch &#187; Javascript</title>
	<atom:link href="http://www.quietless.com/kitchen/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.quietless.com/kitchen</link>
	<description>The choreography of color and code</description>
	<lastBuildDate>Tue, 27 Jul 2010 19:27:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Format Twitter &#8216;created_at&#8217; Date with Javascript</title>
		<link>http://www.quietless.com/kitchen/format-twitter-created_at-date-with-javascript/</link>
		<comments>http://www.quietless.com/kitchen/format-twitter-created_at-date-with-javascript/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 18:58:56 +0000</pubDate>
		<dc:creator>Stephen Braitsch</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.quietless.com/kitchen/?p=360</guid>
		<description><![CDATA[This is a quick snippet to format the date string returned in the &#8216;created_at&#8217; property of a typical Twitter XML/JSON request. The request returns a string such as : &#8220;Tue Apr 07 22:52:51 +0000 2009&#8243; which you&#8217;ll probably want to localize and clean up before sticking it on your page.
The following function takes the date [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick snippet to format the date string returned in the &#8216;created_at&#8217; property of a typical Twitter XML/JSON request. The request returns a string such as : &#8220;Tue Apr 07 22:52:51 +0000 2009&#8243; which you&#8217;ll probably want to localize and clean up before sticking it on your page.</p>
<p>The following function takes the date returned by Twitter, converts it to local time and formats it to :<br />
Tue Apr 07 • 10:52 PM</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// pass in the 'created_at' string returned from twitter //</span>
<span style="color: #006600; font-style: italic;">// stamp arrives formatted as Tue Apr 07 22:52:51 +0000 2009 //</span>
<span style="color: #003366; font-weight: bold;">function</span> parseTwitterDate<span style="color: #009900;">&#40;</span>$stamp<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>		
<span style="color: #006600; font-style: italic;">// convert to local string and remove seconds and year //		</span>
	<span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>Date.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>$stamp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toLocaleString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// get the two digit hour //</span>
	<span style="color: #003366; font-weight: bold;">var</span> hour <span style="color: #339933;">=</span> date.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// convert to AM or PM //</span>
	<span style="color: #003366; font-weight: bold;">var</span> ampm <span style="color: #339933;">=</span> hour<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">12</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">' AM'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">' PM'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hour<span style="color: #339933;">&gt;</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#41;</span> hour<span style="color: #339933;">-=</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hour<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> hour <span style="color: #339933;">=</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// return the formatted string //</span>
	<span style="color: #000066; font-weight: bold;">return</span> date.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' • '</span> <span style="color: #339933;">+</span> hour <span style="color: #339933;">+</span> date.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">13</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> ampm<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course there are plenty of other ways to do this, including parsing it on the server using php or .net.<br />
If you have a better implementation or suggestion go ahead and post it in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quietless.com/kitchen/format-twitter-created_at-date-with-javascript/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
