<?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>Flex Examples &#187; Date</title>
	<atom:link href="http://blog.flexexamples.com/category/date/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parsing dates with timezones in Flex</title>
		<link>http://blog.flexexamples.com/2009/07/27/parsing-dates-with-timezones-in-flex/</link>
		<comments>http://blog.flexexamples.com/2009/07/27/parsing-dates-with-timezones-in-flex/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:04:14 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[parse()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/?p=1259</guid>
		<description><![CDATA[<p>In a previous example, <a href="http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/">&#8220;Parsing ISO dates with Flex and ActionScript&#8221;</a>, we saw how to parse ISO formatted dates (2008-01-25T06:14:23Z) and convert them into an ActionScript 3.0 Date object.</p> <p>The following example shows how you can convert a Java formatted date (Wed Jul 22 14:29:30 PST 2009) and convert the timezone into an ActionScript [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous example, <a href="http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/">&#8220;Parsing ISO dates with Flex and ActionScript&#8221;</a>, we saw how to parse ISO formatted dates (2008-01-25T06:14:23Z) and convert them into an ActionScript 3.0 Date object.</p>
<p>The following example shows how you can convert a Java formatted date (Wed Jul 22 14:29:30 PST 2009) and convert the timezone into an ActionScript 3.0 friendly format (UTC-0700) so you can use the static <code>Date.parse()</code> method to convert them into an ActionScript 3.0 Date object.</p>
<p>Full code after the jump.</p>
<p><span id="more-1259"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2009/07/27/parsing-dates-with-timezones-in-flex/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> name=<span style="color: #ff0000;">&quot;Date_parse_test&quot;</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            private const datStr1:String = &quot;Wed Jul 22 14:29:30 PST 2009&quot;;</span>
<span style="color: #339933;">            private const datStr2:String = &quot;Wed Jul 22 14:29:30 EDT 2009&quot;;</span>
&nbsp;
<span style="color: #339933;">            private var timeZones:Array = [</span>
<span style="color: #339933;">                /* Hawaii-Aleutian Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;HAST&quot;, zone:&quot;UTC-1000&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;HADT&quot;, zone:&quot;UTC-0900&quot;},</span>
<span style="color: #339933;">                /* Alaska Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;AKST&quot;, zone:&quot;UTC-0900&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;ASDT&quot;, zone:&quot;UTC-0800&quot;},</span>
<span style="color: #339933;">                /* Pacific Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;PST&quot;, zone:&quot;UTC-0800&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;PDT&quot;, zone:&quot;UTC-0700&quot;},</span>
<span style="color: #339933;">                /* Mountain Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;MST&quot;, zone:&quot;UTC-0700&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;MDT&quot;, zone:&quot;UTC-0600&quot;},</span>
<span style="color: #339933;">                /* Central Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;CST&quot;, zone:&quot;UTC-0600&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;CDT&quot;, zone:&quot;UTC-0500&quot;},</span>
<span style="color: #339933;">                /* Eastern Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;EST&quot;, zone:&quot;UTC-0500&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;EDT&quot;, zone:&quot;UTC-0400&quot;},</span>
<span style="color: #339933;">                /* Atlantic Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;AST&quot;, zone:&quot;UTC-0400&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;ADT&quot;, zone:&quot;UTC-0300&quot;},</span>
<span style="color: #339933;">                /* Newfoundland Standard/Daylight Time */</span>
<span style="color: #339933;">                {abbr:&quot;NST&quot;, zone:&quot;UTC-0330&quot;},</span>
<span style="color: #339933;">                {abbr:&quot;NDT&quot;, zone:&quot;UTC-0230&quot;}</span>
<span style="color: #339933;">            ];</span>
&nbsp;
<span style="color: #339933;">            private function simpleDateParser(value:String):Date {</span>
<span style="color: #339933;">                var dat:String = value;</span>
<span style="color: #339933;">                if (isNaN(Date.parse(dat))) {</span>
<span style="color: #339933;">                    var idx:uint;</span>
<span style="color: #339933;">                    var len:uint = timeZones.length</span>
<span style="color: #339933;">                    var obj:Object;</span>
<span style="color: #339933;">                    for (idx=0; idx&lt;len; idx++) {</span>
<span style="color: #339933;">                        obj = timeZones[idx];</span>
<span style="color: #339933;">                        if (dat.indexOf(obj.abbr) &gt; -1) {</span>
<span style="color: #339933;">                            dat = dat.replace(obj.abbr, obj.zone);</span>
<span style="color: #339933;">                            return new Date(Date.parse(dat));</span>
<span style="color: #339933;">                        }</span>
<span style="color: #339933;">                    }</span>
<span style="color: #339933;">                }</span>
<span style="color: #339933;">                // TimeZone not found, or conversion not needed.</span>
<span style="color: #339933;">                return new Date(Date.parse(dat));</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;{datStr1}&quot;</span> fontWeight=<span style="color: #ff0000;">&quot;bold&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> initialize=<span style="color: #ff0000;">&quot;event.currentTarget.text = simpleDateParser(datStr1);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HRule</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;{datStr2}&quot;</span> fontWeight=<span style="color: #ff0000;">&quot;bold&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> initialize=<span style="color: #ff0000;">&quot;event.currentTarget.text = simpleDateParser(datStr2);&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Parsing dates with timezones in Flex on FlexExamples.com',url: 'http://blog.flexexamples.com/2009/07/27/parsing-dates-with-timezones-in-flex/',contentID: 'post-1259',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'parse()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2009/07/27/parsing-dates-with-timezones-in-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parsing ISO dates with Flex and ActionScript</title>
		<link>http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/</link>
		<comments>http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 20:41:46 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[parse()]]></category>
		<category><![CDATA[replace()]]></category>
		<category><![CDATA[toLocaleString()]]></category>
		<category><![CDATA[toUTCString()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/</guid>
		<description><![CDATA[<p>The following example shows how you can parse an ISO format date (for more information, see <a href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime</a>) and convert it into a Date object in ActionScript using the String class&#8217;s replace() method and the static Date.parse() method.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Date_parse_test/main.mxml">View MXML</a></p> &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;!-- http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/ --&#62; &#60;mx:Application [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can parse an ISO format date (for more information, see <a href="http://www.w3.org/TR/NOTE-datetime">http://www.w3.org/TR/NOTE-datetime</a>) and convert it into a Date object in ActionScript using the String class&#8217;s <code>replace()</code> method and the static <code>Date.parse()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-493"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Date_parse_test/main.mxml">View MXML</a></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init();&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            [Bindable]</span>
<span style="color: #339933;">            private var d:Date;</span>
&nbsp;
<span style="color: #339933;">            private function init():void {</span>
<span style="color: #339933;">                var isoStr:String = &quot;2008-01-25T06:14:23Z&quot;;</span>
<span style="color: #339933;">                d = isoToDate(isoStr);</span>
<span style="color: #339933;">                formHeading.label = isoStr;</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function isoToDate(value:String):Date {</span>
<span style="color: #339933;">                var dateStr:String = value;</span>
<span style="color: #339933;">                dateStr = dateStr.replace(/\-/g, &quot;/&quot;);</span>
<span style="color: #339933;">                dateStr = dateStr.replace(&quot;T&quot;, &quot; &quot;);</span>
<span style="color: #339933;">                dateStr = dateStr.replace(&quot;Z&quot;, &quot; GMT-0000&quot;);</span>
<span style="color: #339933;">                return new Date(Date.parse(dateStr));</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Form</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormHeading</span> id=<span style="color: #ff0000;">&quot;formHeading&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;toString():&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;{d.toString()}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;toLocaleString():&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;{d.toLocaleString()}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;toUTCString():&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;{d.toUTCString()}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Form</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Date_parse_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Date_parse_test/bin/main.html" width="100%" height="180"></iframe></p>
<p class="note">A big thanks to Jon for helping me with the bug in my time-zone conversion.</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Parsing ISO dates with Flex and ActionScript on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/',contentID: 'post-493',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'parse(),replace(),toLocaleString(),toUTCString()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/02/02/parsing-iso-dates-with-flex-and-actionscript/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Date math for lazy people</title>
		<link>http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/</link>
		<comments>http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/#comments</comments>
		<pubDate>Fri, 24 Aug 2007 18:30:33 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Date]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/</guid>
		<description><![CDATA[<p>I&#8217;ve always hated doing simple date manipulation and having to create wacky variables like &#8220;millisecondsPerDay&#8221; where you multiply milliseconds by seconds by hours by whatever else. So here&#8217;s a useful (and largely untested) function, dateAdd(), which is similar to the function that ColdFusion-ers have enjoyed for years.</p> <p>Full code (and instructions) after the jump.</p> <p></p> [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always hated doing simple date manipulation and having to create wacky variables like &#8220;millisecondsPerDay&#8221; where you multiply milliseconds by seconds by hours by whatever else. So here&#8217;s a useful (and largely untested) function, <code>dateAdd()</code>, which is similar to the function that ColdFusion-ers have enjoyed for years.</p>
<p>Full code (and instructions) after the jump.</p>
<p><span id="more-119"></span></p>
<p>In addition to my code being untested, I should point out that the datepart strings are different than those in ColdFusion. Where ColdFusion uses formats such as: &#8220;yyyy&#8221;, &#8220;q&#8221;, &#8220;m&#8221;, &#8220;y&#8221;, &#8220;d&#8221;, &#8220;w&#8221;, etc. I&#8217;ve cut corners and used the property names from the Date class (&#8220;fullYear&#8221;, &#8220;month&#8221;, &#8220;date&#8221;, &#8220;hours&#8221;, &#8220;minutes&#8221;, &#8220;seconds&#8221;, &#8220;milliseconds&#8221;) instead. This let me use:</p>
<pre class="code">returnDate[datepart] += number;</pre>
<p>Which is basically a shorthand for something like:</p>
<pre class="code">returnDate[&quot;<i>months</i>&quot;] += 3;</pre>
<p class="note">If you wanted to use the ColdFusion type notation it should be easy enough to extend the <code>dateAdd()</code> function and add another simple <code>switch</code> statement which converts the &#8220;yyyy&#8221; to &#8220;fullYear&#8221;, and so on.</p>
<p>Using the <code>dateAdd()</code> function is [hopefully] pretty straight forward.</p>
<ul>
<li>All three parameters are optional. If no parameters are passed, the current date is returned (similar to the Date class constructor).</li>
<li>If only the first parameter (<code>datepart</code>) is passed, the current date is returned.</li>
<li>If the first two parameters (<code>datepart</code> and <code>number</code>) are passed, the function adds the specified number of date parts to the current date. For example, if you called <code>dateAdd("month", 3)</code> the function would return the current date plus 3 months. If you called <code>dateAdd("month", -3)</code> the function would return the current date minus 3 months.</li>
<li>If the first three parameters (<code>datepart</code>, <code>number</code>, and <code>date</code>) are passed, the function adds the specified number of date parts to the specified date. For example, if you called <code>dateAdd("day", 15, <i>calendarStartDate</i>)</code> the function would return the value of the <code>calendarStartDate</code> variable (which must be a Date object) plus 15 days.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/ --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            import mx.controls.dataGridClasses.DataGridColumn;</span>
<span style="color: #339933;">            private function init():void {</span>
<span style="color: #339933;">                var zeroDate:Date = new Date(0);</span>
&nbsp;
<span style="color: #339933;">                /* fullYear */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+2 years&quot;, data:dateAdd(&quot;fullYear&quot;, 2, zeroDate)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-2 years&quot;, data:dateAdd(&quot;fullYear&quot;, -2, zeroDate)});</span>
<span style="color: #339933;">                /* month */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+11 months&quot;, data:dateAdd(&quot;month&quot;, 11)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-11 months&quot;, data:dateAdd(&quot;month&quot;, -11)});</span>
<span style="color: #339933;">                /* date */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+4 date&quot;, data:dateAdd(&quot;date&quot;, 4)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-4 date&quot;, data:dateAdd(&quot;date&quot;, -4)});</span>
<span style="color: #339933;">                /* hours */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+6 hours&quot;, data:dateAdd(&quot;hours&quot;, 6)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-6 hours&quot;, data:dateAdd(&quot;hours&quot;, -6)});</span>
<span style="color: #339933;">                /* minutes */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+45 minutes&quot;, data:dateAdd(&quot;minutes&quot;, 45)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-45 minutes&quot;, data:dateAdd(&quot;minutes&quot;, -45)});</span>
<span style="color: #339933;">                /* seconds */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+900 seconds&quot;, data:dateAdd(&quot;seconds&quot;, 900)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-900 seconds&quot;, data:dateAdd(&quot;seconds&quot;, -900)});</span>
<span style="color: #339933;">                /* milliseconds */</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;+720000 milliseconds&quot;, data:dateAdd(&quot;milliseconds&quot;, 720000)});</span>
<span style="color: #339933;">                arrColl.addItem({label:&quot;-720000 milliseconds&quot;, data:dateAdd(&quot;milliseconds&quot;, -720000)});</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function dateAdd(datepart:String = &quot;&quot;, number:Number = 0, date:Date = null):Date {</span>
<span style="color: #339933;">                if (date == null) {</span>
<span style="color: #339933;">                    /* Default to current date. */</span>
<span style="color: #339933;">                    date = new Date();</span>
<span style="color: #339933;">                }</span>
&nbsp;
<span style="color: #339933;">                var returnDate:Date = new Date(date.time);;</span>
&nbsp;
<span style="color: #339933;">                switch (datepart.toLowerCase()) {</span>
<span style="color: #339933;">                    case &quot;fullyear&quot;:</span>
<span style="color: #339933;">                    case &quot;month&quot;:</span>
<span style="color: #339933;">                    case &quot;date&quot;:</span>
<span style="color: #339933;">                    case &quot;hours&quot;:</span>
<span style="color: #339933;">                    case &quot;minutes&quot;:</span>
<span style="color: #339933;">                    case &quot;seconds&quot;:</span>
<span style="color: #339933;">                    case &quot;milliseconds&quot;:</span>
<span style="color: #339933;">                        returnDate[datepart] += number;</span>
<span style="color: #339933;">                        break;</span>
<span style="color: #339933;">                    default:</span>
<span style="color: #339933;">                        /* Unknown date part, do nothing. */</span>
<span style="color: #339933;">                        break;</span>
<span style="color: #339933;">                }</span>
<span style="color: #339933;">                return returnDate;</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            private function data_labelFunc(item:Object, column:DataGridColumn):String {</span>
<span style="color: #339933;">                return dateFormatter.format(item[column.dataField]);</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:ArrayCollection</span> id=<span style="color: #ff0000;">&quot;arrColl&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DateFormatter</span> id=<span style="color: #ff0000;">&quot;dateFormatter&quot;</span></span>
<span style="color: #000000;">            formatString=<span style="color: #ff0000;">&quot;YYYY/MM/DD HH:NN:SS&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> text=<span style="color: #ff0000;">&quot;Now: {dateAdd()}&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGrid</span> id=<span style="color: #ff0000;">&quot;dataGrid&quot;</span></span>
<span style="color: #000000;">            dataProvider=<span style="color: #ff0000;">&quot;{arrColl}&quot;</span></span>
<span style="color: #000000;">            sortableColumns=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #000000;">            width=<span style="color: #ff0000;">&quot;400&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;label&quot;</span></span>
<span style="color: #000000;">                    headerText=<span style="color: #ff0000;">&quot;Label&quot;</span></span>
<span style="color: #000000;">                    width=<span style="color: #ff0000;">&quot;150&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;data&quot;</span></span>
<span style="color: #000000;">                    headerText=<span style="color: #ff0000;">&quot;YYYY/MM/DD HH:NN:SS&quot;</span></span>
<span style="color: #000000;">                    labelFunction=<span style="color: #ff0000;">&quot;data_labelFunc&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:DataGrid</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Date_test_2/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Date_test_2/bin/main.html" width="100%" height="250"></iframe></p>
<p>As a bonus tip&#8230; I believe you could do this with the Date constructor in ActionScript 2.0 as well, but I always find this handy. The following snippet creates a new Date object for January 45th, 2007. ActionScript cleverly adjusts that for you and returns the date of February 14th, 2007.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">const JANUARY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</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;">// ...</span>
const DECEMBER<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">11</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">d</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Date</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2007</span><span style="color: #000066; font-weight: bold;">,</span> JANUARY<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">45</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toDateString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">// Wed Feb 14 2007</span></pre></div></div>

<p>Although DO note that the <code><i>Date</i>.toDateString()</code> method (as well as uint data type and const keyword) are specific to ActionScript 3.0.</p>
<p>Happy Flexing!</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Date math for lazy people on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/',contentID: 'post-119',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: '',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Calculating the number of days between two selected dates in a DateChooser control</title>
		<link>http://blog.flexexamples.com/2007/07/31/calculating-the-number-of-days-between-two-selected-dates-in-a-datechooser-control/</link>
		<comments>http://blog.flexexamples.com/2007/07/31/calculating-the-number-of-days-between-two-selected-dates-in-a-datechooser-control/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 04:54:09 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[Date]]></category>
		<category><![CDATA[DateChooser]]></category>
		<category><![CDATA[DateFormatter]]></category>
		<category><![CDATA[dataField]]></category>
		<category><![CDATA[rangeEnd]]></category>
		<category><![CDATA[rangeStart]]></category>
		<category><![CDATA[selectableRange]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/07/31/calculating-the-number-of-days-between-two-selected-dates-in-a-datechooser-control/</guid>
		<description><![CDATA[<p>Here&#8217;s a marginally more interesting sample. I was playing around with the DateChooser (calendar) control and the selectableRange property and figured I&#8217;d make a simple application to calculate the differences between two selected dates. The example creates a selectable range of dates from the current date to current date plus 1 year. Users can select [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a marginally more interesting sample. I was playing around with the DateChooser (calendar) control and the <code>selectableRange</code> property and figured I&#8217;d make a simple application to calculate the differences between two selected dates. The example creates a selectable range of dates from the current date to current date plus 1 year. Users can select any single or range of dates between the start date and end date, and Flex calculates the number of days the user selected using some basic math.</p>
<p><span id="more-43"></span></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> name=<span style="color: #ff0000;">&quot;DateChooser_selectableRange_test&quot;</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        layout=<span style="color: #ff0000;">&quot;vertical&quot;</span></span>
<span style="color: #000000;">        verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">        backgroundColor=<span style="color: #ff0000;">&quot;white&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">        &lt;![CDATA[</span>
<span style="color: #339933;">            import mx.controls.dataGridClasses.DataGridColumn;</span>
&nbsp;
<span style="color: #339933;">            /* Number of milliseconds in a day. (1000 milliseconds per second * 60 seconds per minute * 60 minutes per hour * 24 hours per day) */</span>
<span style="color: #339933;">            private const MS_PER_DAY:uint = 1000 * 60 * 60 * 24;</span>
&nbsp;
<span style="color: #339933;">            [Bindable]</span>
<span style="color: #339933;">            private var startDate:Date = new Date();</span>
&nbsp;
<span style="color: #339933;">            [Bindable]</span>
<span style="color: #339933;">            private var endDate:Date = new Date(startDate.fullYear + 1, startDate.month, startDate.date);</span>
&nbsp;
<span style="color: #339933;">            /* Default label function for the DataGrid. Basically just calls the DateFormatter on the current column's contents. */</span>
<span style="color: #339933;">            private function cellDateFormatter(item:Object, column:DataGridColumn):String {</span>
<span style="color: #339933;">                var columnDataField:String = column.dataField;</span>
<span style="color: #339933;">                return dateFormatter.format(item[columnDataField]);</span>
<span style="color: #339933;">            }</span>
&nbsp;
<span style="color: #339933;">            /* Custom label function for the last column. Calculates the number of days between the start date and end date. */</span>
<span style="color: #339933;">            private function calculateDays(item:Object, column:DataGridColumn):String {</span>
<span style="color: #339933;">                var tempDate:Date = new Date(item.rangeEnd - item.rangeStart);</span>
<span style="color: #339933;">                return Math.round((tempDate.time / MS_PER_DAY) + 1).toString();</span>
<span style="color: #339933;">            }</span>
<span style="color: #339933;">        ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DateFormatter</span> id=<span style="color: #ff0000;">&quot;dateFormatter&quot;</span> formatString=<span style="color: #ff0000;">&quot;MMM D, YYYY&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DateChooser</span> id=<span style="color: #ff0000;">&quot;dateChooser&quot;</span> selectableRange=<span style="color: #ff0000;">&quot;{{rangeStart:startDate, rangeEnd:endDate}}&quot;</span> allowMultipleSelection=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGrid</span> id=<span style="color: #ff0000;">&quot;selDates&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{dateChooser.selectedRanges}&quot;</span> labelFunction=<span style="color: #ff0000;">&quot;cellDateFormatter&quot;</span> verticalScrollPolicy=<span style="color: #ff0000;">&quot;on&quot;</span> height=<span style="color: #ff0000;">&quot;{dateChooser.height}&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;rangeStart&quot;</span> headerText=<span style="color: #ff0000;">&quot;Range Start:&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> dataField=<span style="color: #ff0000;">&quot;rangeEnd&quot;</span> headerText=<span style="color: #ff0000;">&quot;Range End:&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:DataGridColumn</span> labelFunction=<span style="color: #ff0000;">&quot;calculateDays&quot;</span> headerText=<span style="color: #ff0000;">&quot;Number Days:&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:columns</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:DataGrid</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<div span="googleAdsLeaderboard">
<script type="text/javascript"><!--
google_ad_client = "pub-3325829455487492";
/* 728x90, created 7/15/09 */
google_ad_slot = "6403511741";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />
</div>
<p class="information">View source enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/DateChooser_selectableRange_test/bin/main.html" width="100%" height="250"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Calculating the number of days between two selected dates in a DateChooser control on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/07/31/calculating-the-number-of-days-between-two-selected-dates-in-a-datechooser-control/',contentID: 'post-43',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'dataField,rangeEnd,rangeStart,selectableRange',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/07/31/calculating-the-number-of-days-between-two-selected-dates-in-a-datechooser-control/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

