3 Replies Latest reply on Apr 4, 2007 11:25 AM by peelmaster

    Converting Date from Integer to String

      So what I'm trying to do is convert the results of a query. What happens is that my data comes in where the month is displayed as an integer from 1-12. I want to convert these numbers to string months like Jan, Feb, Mar, and so on. I've been trying to use the DateFormatter, but I'm guessing that this isn't working because it doesn't like the format that the data is coming to it in. It's easier to explain by looking at my code:

      Here's my function to handle the date:
      public function fnShowMonth(item:Object,column:DataGridColumn):String
      return dfShowMonth.format(item[column.dataField]);

      Here is the Date Formatter:
      <mx:DateFormatter id="dfShowMonth" formatString="MMMM"/>

      Here is the DataGrid I'm using for output:
      <mx:DataGridColumn dataField="MONTH TOTAL"/>
      <mx:DataGridColumn dataField="MONTH1" labelFunction="fnShowMonth" />
      <mx:DataGridColumn dataField="YEAR1" />

      I've used this method for formatting other fields, but not a date field in particular. All I really want to do is convert these number outputs into a text month in my datagrid. Right now this just gives me an empty "MONTH1" column. I can get the numbers when I remove the labelFunction. Any ideas what I need to do?
        • 1. Re: Converting Date from Integer to String
          // this is rather tacky, but it functions. You'll want to provide bounds checking

          public static var months : Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

          public function fnShowMonth(column:DataGridColumn):String
          return months[column.dataField-1];

          // joe
          • 2. Re: Converting Date from Integer to String
            peelmaster Level 1
            Hmm, this would definitely appear to do the job, so why do I get a "1067: Implicit coercion of a value of type String to an unrelated type Number" error?
            • 3. Re: Converting Date from Integer to String
              peelmaster Level 1
              To get around the coercion of data types...I just adapted your suggestion to...

              public static var months : Array = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

              public function fnShowMonth(item:Object, column:DataGridColumn):String
              return months[item[column.dataField]];

              A bit of a messy way to do it for sure. But it beats me spending all day on this. I had to add the blank extra entry in the array, since Flex (as it should) doesn't want me performing mathmatical functions on data types that aren't explicitly listed as numbers. To clean it up, I believe all I really have to do is recast the data when it comes in as a number, then convert it to a string later. If anyone has any other suggestions, I'm all ears.