【问题标题】:Flex: Display month name in DateField controlFlex:在 DateField 控件中显示月份名称
【发布时间】:2011-06-30 09:09:26
【问题描述】:

我在 Flex 中使用 MX DateField 控件,并希望将日期显示为 2011 年 7 月 1 日或 2011 年 7 月 1 日。有人知道该怎么做吗?我尝试将 formatString 设置为“DD MMM YYYY”,但没有成功。

【问题讨论】:

    标签: apache-flex flex4.5 datefield


    【解决方案1】:

    这行得通:

    <fx:Declarations>
        <mx:DateFormatter id="myDf" formatString="DD MMM YYYY"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function formatDate(date:Date):String{
                return myDf.format(date);
            }
        ]]>
    </fx:Script>
    <mx:DateField id="dateField" labelFunction="formatDate" />
    

    http://livedocs.adobe.com/flex/3/html/help.html?content=controls_12.html 的 LiveDocs 中找到它

    但这并不能解释为什么组件上的 formatString 属性不能正常工作。 我可以确认它没有按预期工作。

    干杯

    【讨论】:

    • 我认为原因是 MX DateField 使用其(不太强大的)内部函数 dateToString() 和 stringToDate() 来解析和格式化值,而不是使用默认的格式化程序。
    • 我认为这是long-standing Flex bug的结果; dateToString()stringToDate() 并没有像 &lt;mx:DateFormatter/&gt; 元素中使用的代码那样功能齐全。此错误还意味着您必须在更改labelFunction() 时将parseFunction() 设置为null(请参阅错误报告)。
    【解决方案2】:

    我会使用这样的东西:

    <mx DateField id        = "dateField" 
                  dayNames  ="["S", "M", "T", "W", "T", "F", "S"]"
                  monthNames="["January", "February", "March", "April", "May",
                               "June", "July", "August", "September", "October", 
                               "November", "December"]" />
    

    既然你提到了 3 个字符的月份名称,这是一个很好的例子。当然,如果您不需要日期名称,请删除该行。

    <mx DateField id        = "dateField" 
                  dayNames  ="["S", "M", "T", "W", "T", "F", "S"]"
                  monthNames="["Jan", "Feb", "Mar", "Apr", "May",
                               "Jun", "Jul", "Aug", "Sep", "Oct", 
                               "Nov", "Dec"]" 
               formatString = "DD MMM YYY" />
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2023-03-20
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      相关资源
      最近更新 更多