<?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>Blue Flex &#187; Dates</title>
	<atom:link href="http://kennethsutherland.com/tag/dates/feed/" rel="self" type="application/rss+xml" />
	<link>http://kennethsutherland.com</link>
	<description>Flex with a hint of cool</description>
	<lastBuildDate>Wed, 18 Apr 2012 21:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Dates, how many days? (DateValidator)</title>
		<link>http://kennethsutherland.com/2009/05/25/dates-how-many-days-datevalidator/</link>
		<comments>http://kennethsutherland.com/2009/05/25/dates-how-many-days-datevalidator/#comments</comments>
		<pubDate>Mon, 25 May 2009 15:43:51 +0000</pubDate>
		<dc:creator>Kenneth</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[DateValidator]]></category>
		<category><![CDATA[ValidationResultEvent]]></category>

		<guid isPermaLink="false">http://kennethsutherland.com/?p=389</guid>
		<description><![CDATA[It&#8217;s been a while since I last looked at this, and it annoys me that there isn&#8217;t a simple function to do this. So if you are wishing to find out how many days are in a month for a given year then feel free to use the below. Of course it&#8217;s going to be [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I last looked at this, and it annoys me that there isn&#8217;t a simple function to do this. So if you are wishing to find out how many days are in a month for a given year then feel free to use the below.</p>
<p>Of course it&#8217;s going to be virtually the same result for most years, but I still think it should be a function inside the Date class (or maybe a DateUtil class).</p>
<p>Start off with a standard Switch statement as all months (apart from February) have a fixed number of days.  This of course isn&#8217;t exactly rocket science but how do we figure out how many days February has?</p>
<p>Well the solution still isn&#8217;t rocket science but I like it, if you create a DateValidator and you give it a date of 29/02/2003 (yes this is a UK date &#8211; date at the start) and get it to validate that, then it will fail as 2003 isn&#8217;t a leap year. So February for 2003 must only have 28 days.</p>
<p>That’s it.  You could of course just divide the year by 4 and check to see if there is a modulus of 0, if so then it is a leap year.  If you chose to use this approach you&#8217;d need to make sure that the date range you are using doesn&#8217;t include anything unusual (I&#8217;m not sure how constant the leap year really is, if it&#8217;s anything like the clocks going forward or back 1 or 2 hours then you&#8217;re best just using the internal date validation).  The flash player gets its time from the operation system, so (AFAIK) its rules for working out if a date is valid is also comes from the operating system.</p>
<p>the below code also shows an example of using the DateValidator that doesn&#8217;t use the month/date/year input, which is another reason why I like it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getNumberOfDaysInMonth<span style="color: #000000;">&#40;</span><span style="color: #004993;">month</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">year</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">Number</span><span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">switch</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">month</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//January</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//March</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//May</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//July</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">7</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//August</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">9</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//October</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">11</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//December</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">31</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//April</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//June</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//September</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//November </span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">30</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #009900; font-style: italic;">//February</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> getNumberOfDaysInFebruary<span style="color: #000000;">&#40;</span><span style="color: #004993;">year</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//</span>
    <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">default</span><span style="color: #000066; font-weight: bold;">:</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//should never reach this - if it does then 0 is an error</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> getNumberOfDaysInFebruary<span style="color: #000000;">&#40;</span><span style="color: #004993;">year</span>  <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> <span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
&nbsp;
  <span style="color: #6699cc; font-weight: bold;">var</span> isValid <span style="color: #000066; font-weight: bold;">:</span> DateValidator = <span style="color: #0033ff; font-weight: bold;">new</span> DateValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  isValid<span style="color: #000066; font-weight: bold;">.</span>inputFormat = <span style="color: #990000;">&quot;DD/MM/YYYY&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
  isValid<span style="color: #000066; font-weight: bold;">.</span>allowedFormatChars = <span style="color: #990000;">&quot;/&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> result <span style="color: #000066; font-weight: bold;">:</span> ValidationResultEvent = isValid<span style="color: #000066; font-weight: bold;">.</span>validate<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;29/02/&quot;</span><span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">year</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>result<span style="color: #000066; font-weight: bold;">.</span>results == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900; font-style: italic;">//29th is a valid date</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">29</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900; font-style: italic;">//29th is NOT a valid date</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">28</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7396620608505330";
google_ad_slot = "1277482570";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://kennethsutherland.com/2009/05/25/dates-how-many-days-datevalidator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

