4 Replies Latest reply on Jun 4, 2010 6:57 AM by Sebastien V.

    Date Formatter

    asifdegr8 Level 1

      I have a date in format

      2009-05-04 12:04:00.0

       

      How to convert this in to format

      May 2009

        • 1. Re: Date Formatter
          BhaskerChari Level 4

          Hi,

           

          Use the below code:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="
          http://www.adobe.com/2006/mxml">

              <mx:Script>
                  <![CDATA[

                      import mx.events.ValidationResultEvent;           
                      private var vResult:ValidationResultEvent;

                      // Event handler to validate and format input.           
                      private function Format():void
                      {
                          vResult = dateVal.validate();
                          if (vResult.type==ValidationResultEvent.VALID) {
                              formattedDate.text=dateFormatter.format(dob.text);
                          }
                       
                          else {
                              formattedDate.text= "";
                          }
                      }
                  ]]>
              </mx:Script>

              <mx:DateFormatter id="dateFormatter" formatString="MMM YYYY"/>

              <mx:DateValidator id="dateVal" source="{dob}" property="text" inputFormat="yyyy-mm-dd"/>

              <mx:Panel width="95%" height="95%"
                      paddingTop="10" paddingLeft="5" paddingRight="5" paddingBottom="10">

                  <mx:Form width="100%">
                      <mx:FormItem label="Enter date (mm/dd/yyyy):" width="100%">
                          <mx:TextInput id="dob" text=""/>
                      </mx:FormItem>

                      <mx:FormItem label="Formatted date: " width="100%">
                          <mx:TextInput id="formattedDate" text="" editable="false"/>
                      </mx:FormItem>

                      <mx:FormItem>
                          <mx:Button label="Validate and Format" click="Format();"/>
                      </mx:FormItem>
                  </mx:Form>

              </mx:Panel>
          </mx:Application>

          If this post answers your question or helps, please kindly mark it as such.


          Thanks,

          Bhasker Chari

           

           

          • 2. Re: Date Formatter
            Sebastien V. Level 3
            var dateFormatter:DateFormatter = new DateFormatter();
            dateFormatter.formatString = "MMMM YYYY"; // of "MMM YYYY" for month truncated to 3 letters
            var formattedDate:String = dateFormatter.format( myDate );
            
            • 3. Re: Date Formatter
              asifdegr8 Level 1

              it doesn't work, bcz my input is a string "2009-05-04 12:04:00.0"

              How to modify such code to work for this code?

              • 4. Re: Date Formatter
                Sebastien V. Level 3
                var dateFormatter:DateFormatter = new DateFormatter();
                dateFormatter.formatString = "MMMM YYYY"; // of "MMM YYYY" for month truncated to 3 letters
                var formattedDate:String = dateFormatter.format(
                "2009-05-04 12:04:00.0" );
                // formattedDate value is "May 2009" here