5 Replies Latest reply on Nov 30, 2009 3:39 PM by Peter deHaan

    DateField one month off

    schipmold

      Has anyone else had any problems with the date of the datefield?

       

      When I set the selectedDate property the month displayed is actually one higher then the month I give it.

      However, when using the date picker it does show the date I selected...

       

      I tried different ways of setting the date, but to no avail...

       

      My code:

       

      startDate = new Date();

      startDate.date = 15;

      startDate.month = 09;

      startDate.fullYear = 2009;

      <mx:DateField editable="true" formatString="DD/MM/YYYY" selectedDate="{startDate}"/>

      output: 15/10/2009
      Thanks
      Skip

        • 1. Re: DateField one month off
          mewk Level 3

          Date::month begins at zero.

          • 2. Re: DateField one month off
            schipmold Level 1

            Ahh thanks a million.

            Can't believe I missed that... I must be getting tired

            • 3. Re: DateField one month off
              mewk Level 3

              it happens

               

              can't blame you though -- day begins at 1 but month at 0??

              • 4. Re: DateField one month off
                Peter deHaan Level 4

                Blame Player.

                 

                Dates are usually a lot easier if you just create some static constants for the Month names:

                 

                <?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">
                    
                    <fx:Script>
                        <![CDATA[
                            [Bindable]
                            private var startDate:Date;
                            
                            private const JAN:uint = 0;
                            private const FEB:uint = 1;
                            private const MAR:uint = 2;
                            private const APR:uint = 3;
                            private const MAY:uint = 4;
                            private const JUN:uint = 5;
                            private const JUL:uint = 6;
                            private const AUG:uint = 7;
                            private const SEP:uint = 8;
                            private const OCT:uint = 9;
                            private const NOV:uint = 10;
                            private const DEC:uint = 11;
                            
                            private function init():void {
                                startDate = new Date(2009, SEP, 15);
                                callLater(df.open);
                            }
                        ]]>
                    </fx:Script>
                    
                    <s:HGroup>
                        <mx:DateField id="df" editable="true" formatString="DD/MM/YYYY" selectedDate="{startDate}"/>
                        <s:Button click="init();" />
                    </s:HGroup>
                    
                </s:Application>
                
                
                
                
                
                
                • 5. Re: DateField one month off
                  schipmold Level 1

                  Thanks for the tip Peter.

                   

                  The thing is that my dates come from a database and are in integer format so I'll try the string format next time

                   

                  All's working fine now, so thanks both.