<?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>17 of 26 &#187; JavaScript</title>
	<atom:link href="http://17of26.com/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://17of26.com</link>
	<description>the world of programming and technology</description>
	<lastBuildDate>Fri, 11 Dec 2009 01:29:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using the JavaScript Date Object</title>
		<link>http://17of26.com/2009/08/using-the-javascript-date-object/</link>
		<comments>http://17of26.com/2009/08/using-the-javascript-date-object/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 01:36:38 +0000</pubDate>
		<dc:creator>Q</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://17of26.com/?p=308</guid>
		<description><![CDATA[Since this is my first JavaScript blog post, I need to take a moment to recommend this book:

As far as I&#8217;m concerned, this is the JavaScript book to get.  It explains the language at a level of detail that you simply won&#8217;t find reading online tutorials and samples.  It covers everything from basic language syntax [...]]]></description>
			<content:encoded><![CDATA[<p>Since this is my first JavaScript blog post, I need to take a moment to recommend this book:</p>
<p><a href="http://www.amazon.com/gp/product/0596101996?ie=UTF8&amp;tag=17of26-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596101996"><img src="/img/amazon/51uHjFsJzpL._SL160_.jpg" border="0" alt="" /></a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=17of26-20&amp;l=as2&amp;o=1&amp;a=0596101996" border="0" alt="" width="1" height="1" /></p>
<p>As far as I&#8217;m concerned, this is <em>the</em> JavaScript book to get.  It explains the language at a level of detail that you simply won&#8217;t find reading online tutorials and samples.  It covers everything from basic language syntax to AJAX.  It has sections on regular expressions, XML, DOM scripting, and client-side graphics.  It also has great reference sections for the various JavaScript objects: Date, Array, Number, etc.  I am constantly referring to it whenever I&#8217;m programming in JavaScript.</p>
<p>Anyway, the purpose of this post is to show a couple of neat things that I discovered about the JavaScript Date object that aren&#8217;t readily apparent from most of the samples and tutorials that you&#8217;ll find.  w3schools.com has a pretty good <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp">Date object reference</a> if you need one.</p>
<p>One thing that you need to keep in mind when dealing with the JavaScript Date object is that the integer value for months is 0 based, not 1 based.  This means that January = 0, February = 1, etc.  For example, the following code:</p>
<pre class="syntax-highlight:javascript">var today = new Date();  // Creates a date object set to the current date and time
alert( today.toDateString() + &quot;\nMonth integer = &quot; + today.getMonth());</pre>
<p>Will show the following message:</p>
<p><img class="aligncenter size-full wp-image-309" title="dateinteger" src="http://17of26.com/wp-content/uploads/2009/08/dateinteger.png" alt="dateinteger" width="365" height="138" /></p>
<p>What&#8217;s really cool about the JavaScript Date object is that it does date math for you.  For example, we could set a date object to be January 1, 2009 and subtract 7 days from it:</p>
<pre class="syntax-highlight:javascript">var jan1_2009 = new Date(2009, 0, 1);  // Creates a date object set to the January 1, 2009
var oneWeekBefore = new Date(2009, 0, 1 - 7);  // Creates a date object set to the January 1, 2009 minus 7 days

alert(&quot;One week before &quot; + jan1_2009.toDateString() + &quot; is &quot; + oneWeekBefore.toDateString());

var oneWeekBefore2 = new Date(2009, 0, 1);  // Creates a date object set to the January 1, 2009
oneWeekBefore2.setDate( oneWeekBefore2.getDate() - 7);  // Subtract 7 days

alert(&quot;One week before &quot; + jan1_2009.toDateString() + &quot; is &quot; + oneWeekBefore.toDateString());</pre>
<p>Both alerts in the code above would show the following:</p>
<p><img class="aligncenter size-full wp-image-311" title="oneweekdate" src="http://17of26.com/wp-content/uploads/2009/08/oneweekdate.png" alt="oneweekdate" width="365" height="138" /></p>
<p>You can also use the Date object to figure out how many days are in a given month.  This is particularly useful for determining how many days are in February for a given year.  When you create a Date object with a year, month, and a day of 0 then Date object automatically sets itself to the last day of the previous month.  Remembering that month integers are zero based:</p>
<pre class="syntax-highlight:javascript">var day = new Date(2009, 2, 0);  // Sets the date to the 0th day in March, which is the last day in February.

alert(day.toDateString() + &quot; : February of 2009 has &quot; + day.getDate() + &quot; days.&quot;);</pre>
<p>This will display:</p>
<p><img class="aligncenter size-full wp-image-314" title="daysinmonth" src="http://17of26.com/wp-content/uploads/2009/08/daysinmonth.png" alt="daysinmonth" width="365" height="138" /></p>
]]></content:encoded>
			<wfw:commentRss>http://17of26.com/2009/08/using-the-javascript-date-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
