【问题标题】:How do I change SelectedDateFormat to display 21 Nov 2019?如何将 SelectedDateFormat 更改为显示 2019 年 11 月 21 日?
【发布时间】:2020-03-17 16:31:49
【问题描述】:

如何将 SelectedDateFormat 更改为 2019 年 11 月 21 日?

<DatePicker CalendarStyle="{StaticResource resizedCalendarItem}" 
    SelectedDateFormat="Short" 
    x:Name="gDPickVisitDate" 
    SelectedDateChanged="gDPickVisitDate_SelectedDateChanged" />

显示 2019 年 11 月 21 日。

请注意,Changing the string format of the WPF DatePicker 会在对话框中显示“2019 年 11 月 21 日”。

但是,string stDate = gDPickVisitDate.ToString 导致 stDate 包含“11/21/2019 12:00:00 AM"”。

编译器不喜欢gDPickVisitDate.Value。我尝试了GetValue,但没有成功。

【问题讨论】:

  • 是什么意思,但是 gDPickVisitDate 仍然包含 11/21/2019。?如果更改格式显示正确,您还在寻找什么?
  • From the docs "默认的 DateTime.ToString() 方法使用当前区域性的短日期和长时间模式返回日期和时间值的字符串表示形式。"
  • 如果您希望 DateTime 对象的 ToString 采用您的格式,您必须指定它 gDPickVisitDate.Value.ToString("dd MMM yyyy")

标签: c# wpf


【解决方案1】:

最简单的解决方案是:

gDPickVisitDate.ToString("dd MMM yyy"));
//dd/MMM/yyyy: 21 Nov 2019

或如 Microsoft here 所述

 //Create a new DateTimeFormatInfo where you also can set your CultureInfo
 DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
 //Choose the separator
 dtfi.DateSeparator = " ";
 //Choose the format
 dtfi.ShortDatePattern = @"dd/MMM/yyyy";
 //Write it to the console
 Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern, 
                                   gDPickVisitDate.ToString("d", dtfi));

// The example displays the following output:
//       Original Short Date Pattern:
//          dd/MMM/yyyy: 21 Nov 2019

如果您想知道可能的格式或输出,请查看 Microsoft 的 this 站点。

【讨论】:

    【解决方案2】:

    感谢 Peter Fleischer => https://social.msdn.microsoft.com/Forums/vstudio/en-US/3ac5b308-5bd7-4a6b-bf92-1ab4ebe258b1/how-do-i-change-selecteddateformat-to-display-21-nov-2019?forum=wpf

    string stDate = string.Empty;
    if (gDPickVisitDate.SelectedDate.HasValue)
      stDate = gDPickVisitDate.SelectedDate.Value.ToString("dd MMM yyyy");
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 2015-02-23
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多