Uploaded image for project: 'Apache Flex'
  1. Apache Flex
  2. FLEX-20017

DateFormatter MMM & MMMM return month of by 1

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Not A Problem
    • Adobe Flex SDK 3.2 (Release)
    • None
    • Formatters
    • Affected OS(s): All OS Platforms
      Affected OS(s): All OS Platforms
      Language Found: English

    Description

      var df:DateFormatter = new DateFormatter();
      df.formatString = "DD MMM YY"
      trace(df.format(new Date(2000,1,1)))

      You get 01 Feb 00 when you should get 01 Jan 00

      The problem is in DateBase.extractTokenDate()
      It looks up DateBase.monthNamesShort|monthNamesLong[month] when month is 1 based, not zero based like the array.
      The case where (key < 3) realises this, and does month++

      I'm using the Eclipse Plugin on MacOS.

      This patch fixes it (use it in place of DateFormatter)

      package
      {
      import mx.formatters.DateFormatter;
      import mx.core.mx_internal;
      import mx.managers.ISystemManager;
      import mx.managers.SystemManager;
      import mx.formatters.DateBase;
      import mx.formatters.StringFormatter;

      public class DateFormatterPatched extends DateFormatter
      {

      private static const VALID_PATTERN_CHARS:String = "Y,M,D,A,E,H,J,K,L,N,S";

      public function DateFormatterPatched()

      { super(); }

      mx_internal static function extractTokenDate(date:Date,
      tokenInfo:Object):String
      {
      //initialize();

      //var result:String = "";

      var key:int = int(tokenInfo.end) - int(tokenInfo.begin);

      //var day:int;
      //var hours:int;

      switch (tokenInfo.token) {
      case "M":
      {
      // month in year
      var month:int = int(date.getMonth());
      if (key < 3)

      { return DateBase.mx_internal::extractTokenDate(date,tokenInfo) }

      else if (key == 3)

      { return DateBase.monthNamesShort[month-1]; }

      else

      { return DateBase.monthNamesLong[month-1]; }

      }
      }
      return DateBase.mx_internal::extractTokenDate(date,tokenInfo)
      }

      override public function format(value:Object):String
      {
      // Reset any previous errors.
      if (error)
      error = null;

      // If value is null, or empty String just return ""
      // but treat it as an error for consistency.
      // Users will ignore it anyway.
      if (!value || value == "")

      { error = defaultInvalidValueError; return ""; }

      // – value –

      if (value is String)
      {
      value = DateFormatter.parseDateString(String(value));
      if (!value)
      { error = defaultInvalidValueError; return ""; }
      }
      else if (!(value is Date))
      { error = defaultInvalidValueError; return ""; }

      // – format –

      var letter:String;
      var nTokens:int = 0;
      var tokens:String = "";

      var n:int = formatString.length;
      for (var i:int = 0; i < n; i++)
      {
      letter = formatString.charAt;
      if (VALID_PATTERN_CHARS.indexOf(letter) != -1 && letter != ",")
      {
      nTokens++;
      if (tokens.indexOf(letter) == -1)

      { tokens += letter; }

      else
      {
      if (letter != formatString.charAt(Math.max(i - 1, 0)))

      { error = defaultInvalidFormatError; return ""; }

      }
      }
      }

      if (nTokens < 1)

      { error = defaultInvalidFormatError; return ""; }

      var dataFormatter:StringFormatter = new StringFormatter(
      formatString, VALID_PATTERN_CHARS,
      DateFormatterPatched.mx_internal::extractTokenDate);
      //DateBase.mx_internal::extractTokenDate);

      return dataFormatter.formatValue(value);
      }
      }
      }

      Attachments

        Activity

          People

            jmclean Justin Mclean
            adobejira Adobe JIRA
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: