<?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>Everything old is new again &#187; formatting</title>
	<atom:link href="http://www.svendtofte.com/tag/formatting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.svendtofte.com</link>
	<description>rantings &#38; scraps on code and web development</description>
	<lastBuildDate>Thu, 27 Aug 2009 22:38:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript date string formatting</title>
		<link>http://www.svendtofte.com/javascript/javascript-date-string-formatting/</link>
		<comments>http://www.svendtofte.com/javascript/javascript-date-string-formatting/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 21:23:20 +0000</pubDate>
		<dc:creator>Svend</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[formatting]]></category>

		<guid isPermaLink="false">http://www.svendtofte.com/?p=18</guid>
		<description><![CDATA[Date string formatting is very brutal in JavaScript, which probably has the worst built in library in relation to the distribution this language sees. So a long time ago I whipped up a script file, for doing just this. I always though PHP&#8217;s date function looked very nice, so I decided to model my function [...]]]></description>
			<content:encoded><![CDATA[<p>Date string formatting is very brutal in JavaScript, which probably has the worst built in library in relation to the distribution this language sees. So a long time ago I whipped up a script file, for doing just this. I always though <a href="http://php.net/date">PHP&#8217;s date function</a> looked very nice, so I decided to model my function upon that. </p>
<p><span id="more-18"></span></p>
<p>Download: <a href="http://www.svendtofte.com/wp-content/uploads/2008/07/formatdate.js">formatDate.js</a> (13kB)</p>
<h3>Supported switches and test</h3>
<p>I&#8217;m not going to duplicate <a href="http://php.net/date">PHP&#8217;s documentation</a>. But I will list briefly the supported switches. Basicly, switches should return the same as in PHP, if they don&#8217;t, it&#8217;s most likely in error, so let me know at <script>myEmail();</script>. </p>
<table class="tufteDesign">
<tr>
<td>Supported switches</td>
<td>Unsupported switches</td>
<td>Currently unsupported, but might be included in the future</td>
</tr>
<tr>
<td>a, A, B, c, d, D, F, g, G, h, H, i, I (uppercase i), j, l (lowecase L),  L, m, M, n, N, O, P, r, s, S, t, U, w, W, y, Y, z, Z</td>
<td>T, e, o</td>
<td></td>
</tr>
</table>
<p>Here&#8217;s some examples.</p>
<p><script src="http://www.svendtofte.com/wp-content/uploads/2008/07/formatdate.js"></script><script>
var currDate = new Date();
</script></p>
<table class="tufteDesign">
<tr>
<td>Example</td>
<td>Output</td>
</tr>
<tr>
<td><code>F j, Y, g:i a</code></td>
<td><script>document.write(currDate.formatDate("F j, Y, g:i a"));</script></td>
</tr>
<tr>
<td><code>m.d.y</code></td>
<td><script>document.write(currDate.formatDate("m.d.y"));</script></td>
</tr>
<tr>
<td><code>j, n, Y</code></td>
<td><script>document.write(currDate.formatDate("j, n, Y"));</script></td>
</tr>
<tr>
<td><code>Ymd</code></td>
<td><script>document.write(currDate.formatDate("Ymd"));</script></td>
</tr>
<tr>
<td><code>h-i-s, j-m-y, it is w Day z</code></td>
<td><script>document.write(currDate.formatDate("h-i-s, j-m-y, it is w Day z"));</script></td>
</tr>
<tr>
<td><code>%i%t %i%s %t%h%e jS %d%a%y.</code></td>
<td><script>document.write(currDate.formatDate("%i%t %i%s %t%h%e jS %d%a%y."));</script></td>
</tr>
<tr>
<td><code>D M j G:i:s T Y</code></td>
<td><script>document.write(currDate.formatDate("D M j G:i:s T Y"));</script></td>
</tr>
<tr>
<td><code>H:m:s %m %i%s% %m%o%n%t%h</code></td>
<td><script>document.write(currDate.formatDate("H:m:s %m %i%s% %m%o%n%t%h"));</script></td>
</tr>
<tr>
<td><code>H:i:s</code></td>
<td><script>document.write(currDate.formatDate("H:i:s"));</script></td>
</tr>
</table>
<p>And here you can test your own flags if you should wish.<br />
<script>
function testDateFormat() {
  var inFormat = document.getElementById("dateFormatIn").value;
  var output = document.getElementById("dateFormatOut");
  var d = new Date();
  output.innerHTML = d.formatDate(inFormat);
}
</script></p>
<form onsubmit="return false;">
<p><input type="text" width="20" id="dateFormatIn" value="c"/>
<input type="button" value="hit it!" onclick="testDateFormat();return false;" /> output: <code><span id="dateFormatOut"></span></code></p>
</form>
<h3>Differences and gotchas</h3>
<p>You should note quite quickly that you use <code>%</code> to escape, not backslash (<code>\</code>). PHP gets to treat strings a bit different, and frankly, <code>\\d\\u\\m\\b</code> looks stupid (not to mention the quadruple escape, <code>\\\\</code>, if you want a <em>single</em> backslash in your output).</p>
<p>Another thing to note is that this code operates either directly on a Date object (which always have a time set), or a passed in value, which it interprets as a date. <em>The script doesn&#8217;t help you in any way in setting a time.</em> As a developer, you need to be careful when juggling dates on the client side, and also if you create any dates, don&#8217;t make assumptions regarding what timezone the runtime thinks it is.</p>
<p>I&#8217;ve obsoleted my old page, though <a href="/code/date_format/_index.php">it&#8217;s still on the site under a different name</a>. It had some more rigerous testing (it&#8217;s using the old version of the script so take care), and also some stuff about localization which I don&#8217;t really think is that interesting anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.svendtofte.com/javascript/javascript-date-string-formatting/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
