<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Javascript if date within range</title>
	<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range/</link>
	<description>Comments on Ask MetaFilter post Javascript if date within range</description>
	<pubDate>Wed, 29 Oct 2008 14:27:55 -0800</pubDate>
	<lastBuildDate>Wed, 29 Oct 2008 14:27:55 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Javascript if date within range</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range</link>	
		<description>Javascript: If date is between date range, output this, else output this. &lt;br /&gt;&lt;br /&gt; I need some javascript that outputs certain code if the date is between a certain date range. If it is not, it would output something else.&lt;br&gt;
&lt;br&gt;
Today is 10-29-08&lt;br&gt;
&lt;br&gt;
Date range in script would be something like 10-01-08 to 10-31-08&lt;br&gt;
&lt;br&gt;
If we are in that range, it would output &apos;this code.&apos;&lt;br&gt;
&lt;br&gt;
If we are not in that date range, it would output &apos;different code.&apos;</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2008:site.105522</guid>
		<pubDate>Wed, 29 Oct 2008 13:50:02 -0800</pubDate>
		<dc:creator>B(oYo)BIES</dc:creator>
		
			<category>javascript</category>
		
			<category>date</category>
		
	</item> <item>
		<title>By: bitdamaged</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1523981</link>	
		<description>// Parse the dates. A little tricky because of your format&lt;br&gt;
&lt;br&gt;
leftBound = &quot;10-14-08&quot;;&lt;br&gt;
splitLeftBound = leftBound.split(&quot;-&quot;);&lt;br&gt;
&lt;br&gt;
rightBound = &quot;10-16-08&quot;;&lt;br&gt;
splitRightBound = rightBound.split(&quot;-&quot;);&lt;br&gt;
&lt;br&gt;
testDate = &quot;10-15-08&quot;;&lt;br&gt;
splitDate = testDate.split(&quot;-&quot;);&lt;br&gt;
&lt;br&gt;
// Convert to number of seconds since 1970 - Note this breaks after 2099&lt;br&gt;
leftTest = new Date(2000 + parseFloat(splitLeftBound[2]), splitLeftBound[0] - 1, splitLeftBound[1]).getTime();&lt;br&gt;
rightTest = new Date(2000 + parseFloat(splitRightBound[2]), splitRightBound[0] - 1, splitRightBound[1]).getTime();&lt;br&gt;
test = new Date(2000 + parseFloat(splitDate[2]), splitDate[0] - 1, splitDate[1]).getTime();&lt;br&gt;
&lt;br&gt;
//You now have 3 comparable integers&lt;br&gt;
&lt;br&gt;
if (test &lt;&gt;
	alert(&quot;Outside of left bound&quot;);&lt;br&gt;
} else if (test &amp;gt; rightTest) {&lt;br&gt;
	alert(&quot;Outside of right bound&quot;);&lt;br&gt;
} else {&lt;br&gt;
	alert(&quot;Number is good!&quot;);&lt;br&gt;
}&lt;/&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1523981</guid>
		<pubDate>Wed, 29 Oct 2008 14:27:55 -0800</pubDate>
		<dc:creator>bitdamaged</dc:creator>
	</item><item>
		<title>By: rokusan</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1523982</link>	
		<description>&lt;pre&gt;if (myDate &amp;gt; today) document.write(&apos;today is before myDate&apos;);&lt;br&gt;
else document.write(&apos;today is on or after myDate&apos;); &lt;/pre&gt;&lt;br&gt;
But do you really want to use Javascript for that? That not only requires a Javascript client, it relies on the client&apos;s idea of what day it is.&lt;br&gt;
&lt;br&gt;
Conditional SSI is a lot more robust and under your control.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1523982</guid>
		<pubDate>Wed, 29 Oct 2008 14:28:15 -0800</pubDate>
		<dc:creator>rokusan</dc:creator>
	</item><item>
		<title>By: MaxK</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1524020</link>	
		<description>You&apos;re probably going to want to use a Date object.  I&apos;ve created a demo at my website but I&apos;m not sure what the policy for self-linking is in comments on AskMe, so I&apos;ll post the relevant function here and PM you a link to the full HTML document if you&apos;d like to take a closer look.&lt;br&gt;
&lt;br&gt;
&lt;b&gt;The code:&lt;/b&gt;&lt;br&gt;
&lt;blockquote&gt;&lt;br&gt;
&lt;pre&gt;function dateStringToTime(dateStr) {&lt;br&gt;
	year = dateStr.substring(6, 8);&lt;br&gt;
	month = dateStr.substring(0, 2);&lt;br&gt;
	day = dateStr.substring(3, 5);&lt;br&gt;
	&lt;br&gt;
	return new Date(year, month, day).getTime();&lt;br&gt;
}&lt;/pre&gt;&lt;br&gt;
&lt;/blockquote&gt;&lt;br&gt;
&lt;br&gt;
This will turn your date string into a Unix timestamp.  Then it can be numerically compared numerically with basic operators.  i.e.: &lt;pre&gt;if(date1 &amp;gt; date2) { ... }&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1524020</guid>
		<pubDate>Wed, 29 Oct 2008 14:54:40 -0800</pubDate>
		<dc:creator>MaxK</dc:creator>
	</item><item>
		<title>By: katrielalex</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1524038</link>	
		<description>Second using a Date object: there&apos;s no point sticking the date in a string if there&apos;s a purpose-built container for it. And the Javascript Date object even has a comparison between two dates!&lt;br&gt;
&lt;br&gt;
See &lt;a href=&quot;http://www.w3schools.com/js/js_obj_date.asp&quot;&gt;here&lt;/a&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1524038</guid>
		<pubDate>Wed, 29 Oct 2008 15:14:30 -0800</pubDate>
		<dc:creator>katrielalex</dc:creator>
	</item><item>
		<title>By: bitdamaged</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1524097</link>	
		<description>This function is broken&lt;br&gt;
&lt;pre&gt;&lt;br&gt;
function dateStringToTime(dateStr) {&lt;br&gt;
&lt;br&gt;
	year = dateStr.substring(6, 8); // 08 will not parse correctly as a year&lt;br&gt;
&lt;br&gt;
	month = dateStr.substring(0, 2); // Date function uses months from 0-11&lt;br&gt;
&lt;br&gt;
	day = dateStr.substring(3, 5);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
	return new Date(year, month, day).getTime();&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1524097</guid>
		<pubDate>Wed, 29 Oct 2008 15:58:22 -0800</pubDate>
		<dc:creator>bitdamaged</dc:creator>
	</item><item>
		<title>By: letourneau</title>
		<link>http://ask.metafilter.com/105522/Javascript-if-date-within-range#1524262</link>	
		<description>Confirming bitdamaged. You want:&lt;blockquote&gt;&lt;tt&gt;year&amp;nbsp; = parseInt(dateStr.substring(6, 8), 10);&lt;br&gt;
month = parseInt(dateStr.substring(0, 2), 10) - 1;&lt;br&gt;
day&amp;nbsp;&amp;nbsp; = parseInt(dateStr.substring(3, 5), 10);&lt;/tt&gt;&lt;/blockquote&gt;The parseInt() call turns the string &quot;02&quot; into the integer 2, and the second argument to parseInt() means you want to force JavaScript to use base 10 (it will try to use octal conversion on &quot;08&quot; and &quot;09&quot; otherwise, because of the leading zero, and complain about invalidity).&lt;br&gt;
&lt;br&gt;
Although, if you&apos;re not certain that your month and day pieces are going to be two digits (e.g. if you might get &quot;1-2-2008&quot; for January 2, 2008), you would be better off using split() to break up the string into chunks upon which to run parseInt().&lt;blockquote&gt;&lt;tt&gt;var datePieces = dateStr.split(&quot;-&quot;);&lt;br&gt;
var month = parseInt(datePieces[0], 10) - 1,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;day&amp;nbsp;&amp;nbsp; = parseInt(datePieces[1], 10),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;year&amp;nbsp; = parseInt(datePieces[2], 10);&lt;/tt&gt;&lt;/blockquote&gt;This just covers the string-to-date conversion part of the problem. You&apos;ve gotten the right advice already as to the rest of it: create real Date objects and use those objects&apos; built-in comparison function.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.105522-1524262</guid>
		<pubDate>Wed, 29 Oct 2008 18:55:43 -0800</pubDate>
		<dc:creator>letourneau</dc:creator>
	</item>
	</channel>
</rss>
