3 Replies Latest reply on Apr 26, 2010 9:27 PM by GordonSmith

    Date Calculation Problem

    jfillman Level 1

      The code below correctly calculates the date the every other time the button is clicked.  I would expect to get the same date for both the currentDate and tempDate values, every time I click the button.

       

      Code:

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

       

       

      xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="

      library://ns.adobe.com/flex/spark"

      xmlns:mx="

      library://ns.adobe.com/flex/mx"

      width="

      100%" height="100%" creationComplete="init();">

       

       

      <fx:Script>

      <![CDATA[

       

       

       

      private var currentDate:Date = new Date();

       

      private var tempDate:Date = new Date();

       

      private var newDate:Date;

       

       

      public function calculateDate():void {

      newDate =

      new Date();

      currentDate.fullYear = newDate.fullYear;

      currentDate.month = newDate.month;

      currentDate.date = newDate.date;

      currentDate.hours = 0;

      currentDate.minutes = 0;

      currentDate.seconds = 0;

      currentDate.milliseconds = 0;

       

      trace ("Current Date: "+currentDate);

      tempDate.fullYear = newDate.fullYear;

      tempDate.month = newDate.month;

      tempDate.date = 0;

      tempDate.hours = 0;

      tempDate.minutes = 0;

      tempDate.seconds = 0;

      tempDate.milliseconds = 0;

       

      trace ("Temp Date: "+tempDate);

      }

       

      ]]>

       

      </fx:Script>

       

       

       

      <s:Button click="calculateDate()" label="Click Here" />

      </s:Application>

       

      Trace Output:

           First Click:         

                    Current Date: Thu Apr 22 00:00:00 GMT-0700 2010

                     Temp Date: Wed Mar 31 00:00:00 GMT-0700 2010

           Second Click:

                     Current Date: Thu Apr 22 00:00:00 GMT-0700 2010

                     Temp Date: Fri Apr 30 00:00:00 GMT-0700 2010

           Third Click:

                     Current Date: Thu Apr 22 00:00:00 GMT-0700 2010

                     Temp Date: Wed Mar 31 00:00:00 GMT-0700 2010

           Fourth Click:

                     Current Date: Thu Apr 22 00:00:00 GMT-0700 2010

                     Temp Date: Fri Apr 30 00:00:00 GMT-0700 2010

       

       

        • 1. Re: Date Calculation Problem
          David_F57 Level 5

          hi,

           

          this code works.

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

             xmlns:s="library://ns.adobe.com/flex/spark"

             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

          <fx:Script>

          <![CDATA[

          private var currentDate:Date;

          private var tempDate:Date;

           

          public function calculateDate(myDate:Date):void {

          currentDate = new Date();

          tempDate = new Date();

          currentDate.fullYear = myDate.fullYear;

          currentDate.month = myDate.month;

          currentDate.date = myDate.date;

          currentDate.hours = 0;

          currentDate.minutes = 0;

          currentDate.seconds = 0;

          currentDate.milliseconds = 0;

           

          trace ("Current Date: "+currentDate);

          tempDate.fullYear = myDate.fullYear;

          tempDate.month = myDate.month;

          tempDate.date = 0;

          tempDate.hours = 0;

          tempDate.minutes = 0;

          tempDate.seconds = 0;

          tempDate.milliseconds = 0;

           

          trace ("Temp Date: "+tempDate);

          }

           

          ]]>

           

          </fx:Script>

           

          <s:Button click="calculateDate(new Date())" label="Click Here" />

          </s:Application>

          • 2. Re: Date Calculation Problem
            jfillman Level 1

            The issue occurs when I set tempDate.date = 0

             

            If I set it to 1, and then account for it in calculating the .date vaule, this works correctly.

             

            tempDate.date = 0 alternates between the last day of the previous month and the last day of the current month, for whatever reason. It's odd, maybe a bug, but I can work around it.

            • 3. Re: Date Calculation Problem
              GordonSmith Level 4

              The 1st day of any month is date=1, not date=0. I'm not sure whether the behavior for out-of-range values for date, such as 0, is well-defined by the Ecmascript spec. If it is, you should file a bug.

               

              Gordon Smith

              Adobe Flex SDK Team