【问题标题】:Format Date/Time in XAML in Silverlight在 Silverlight 中的 XAML 中格式化日期/时间
【发布时间】:2011-07-31 21:27:23
【问题描述】:

我有一个 Silverlight 4 应用程序。我有一个绑定到我的 UI 的 C# DateTime 对象。我希望这个 DateTime 的格式为dd/mm/yyyy time (TimeZone)。例如,今天将显示为

04/07/2011 at 01:13 p.m. (EST)

有没有办法做到这一点XAML?还是我需要构建一个转换器?

【问题讨论】:

    标签: c# silverlight xaml


    【解决方案1】:
    <TextBlock Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy a\\t h:mm tt}'}" />
    

    会还给你

    2011 年 4 月 7 日下午 1:28 (-04)

    【讨论】:

    • 我喜欢这些 F~€#~€ 奇怪的字符串格式......谁能回忆起它们(谈论 {}0: 部分)。
    • 这是正确的,除了格式字符串周围的引号应该是单引号
    • 我不得不把我的单引号放在不同的地方:Text="{Binding startDate, StringFormat='{}{0:MM/dd/yy}'}"
    • 如果您使用的是Label 而不是TextBlock,请记住您需要使用ContentStringFormat 而不是设置绑定字符串格式。
    • 所有这些都适用于文本块或文本框,但在日期选择器本身内,格式不起作用。知道如何在日期选择器中进行操作
    【解决方案2】:

    您可以在 Silverlight 4 中使用 StringFormat 为您绑定的值提供自定义格式。

    日期

    日期格式有很多选择。

    日期时间为“2004 年 4 月 17 日下午 1:52:45”

    您既可以使用一组标准格式(标准格式)……

    StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
    StringFormat=g : “4/17/2004 1:52 PM”
    StringFormat=m : “April 17”
    StringFormat=y : “April, 2004”
    StringFormat=t : “1:52 PM”
    StringFormat=u : “2004-04-17 13:52:45Z”
    StringFormat=o : “2004-04-17T13:52:45.0000000”
    

    …或者您可以使用字母(自定义格式)创建自己的日期格式

    StringFormat=’MM/dd/yy’ : “04/17/04”
    StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
    StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”
    

    【讨论】:

      【解决方案3】:

      你也可以只用

      StringFormat=d
      

      在您的数据网格列中显示日期时间

      最终会是

         <sdk:DataGridTextColumn  Binding="{Binding Path=DeliveryDate,StringFormat=d}" Header="Delivery date" Width="*" />
      

      输出看起来像

      【讨论】:

      • 如果我想要日/月/年怎么办?
      • 使用 StringFormat = dd-MMMM-yy
      【解决方案4】:

      在 SL5 中,我发现这是可行的:

      <TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat=dd-MMMM-yy hh:mm}">
      <TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat='Justification Date: \{0:dd-MMMM-yy hh:mm\}'}">
      

      【讨论】:

        【解决方案5】:

        C#:试试这个

        • yyyy(yy/yyy) - 年
        • MM - 月份(如“03”),MMMM - 月份(如“三月”)
        • dd - 天(如 09),ddd/dddd - 天(周日/周日)
        • hh - 12 小时(上午/下午),HH - 24 小时
        • 毫米 - 分钟
        • ss - 秒

        使用一些分隔符,像这样:

        1. MessageBox.Show(DateValue.ToString("yyyy-MM-dd")); 示例结果:“2014-09-30”
        2. 空格式字符串: MessageBox.Show(DateValue.ToString()); 示例结果:“30.09.2014 0:00:00”

        【讨论】:

        • MessageBox.Show(DateValue.ToString()) 此输出取决于执行它的线程的文化(如果未设置其系统文化)
        【解决方案6】:

        对我来说这很有效:

        <TextBlock Text="{Binding Date , StringFormat=g}" Width="130"/>
        

        如果想用 G 代替 g 来显示秒数:

        <TextBlock Text="{Binding Date , StringFormat=G}" Width="130"/>
        

        如果想要将日期类型更改为另一种类型,例如波斯语,请使用语言:

        <TextBlock Text="{Binding Date , StringFormat=G}" Width="130" Language="fa-IR"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-06
          • 2011-11-12
          • 2011-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多