DateTimeFormatInfo.ShortDatePattern 属性

定义

获取或设置短日期值的自定义格式字符串。

public:
 property System::String ^ ShortDatePattern { System::String ^ get(); void set(System::String ^ value); };
public string ShortDatePattern { get; set; }
member this.ShortDatePattern : string with get, set
Public Property ShortDatePattern As String

属性值

短日期值的自定义格式字符串。

例外

该属性设置为 null

设置了该属性,但 DateTimeFormatInfo 对象为只读。

示例

以下示例显示 属性的值 ShortDatePattern ,以及使用 ShortDatePattern 属性设置格式的日期的值,用于一些区域性。

using System;
using System.Globalization;

public class SamplesDTFI
{
   public static void Main()
   {
      string[]  cultures = { "en-US", "ja-JP", "fr-FR" };
      DateTime date1 = new DateTime(2011, 5, 1);

      Console.WriteLine(" {0,7} {1,19} {2,10}\n", "CULTURE", "PROPERTY VALUE", "DATE");

      foreach (var culture in cultures) {
         DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture(culture).DateTimeFormat;
         Console.WriteLine(" {0,7} {1,19} {2,10}", culture,
                           dtfi.ShortDatePattern,
                           date1.ToString("d", dtfi));
      }
   }
}
// The example displays the following output:
//        CULTURE      PROPERTY VALUE       DATE
//
//          en-US            M/d/yyyy   5/1/2011
//          ja-JP          yyyy/MM/dd 2011/05/01
//          fr-FR          dd/MM/yyyy 01/05/2011
Imports System.Globalization

Public Class SamplesDTFI
   Public Shared Sub Main()
      Dim cultures() As String = { "en-US", "ja-JP", "fr-FR" }
      Dim date1 As Date = #05/01/2011#
      
      Console.WriteLine(" {0,7} {1,19} {2,10}", "CULTURE", "PROPERTY VALUE", "DATE")
      Console.WriteLine()
      
      For Each culture As String In cultures
         Dim dtfi As DateTimeFormatInfo = CultureInfo.CreateSpecificCulture(culture).DateTimeFormat
         Console.WriteLine(" {0,7} {1,19} {2,10}", culture, 
                           dtfi.ShortDatePattern, 
                           date1.ToString("d", dtfi))
      Next                     
   End Sub 
End Class
' The example displays the following output:
'        CULTURE      PROPERTY VALUE       DATE
'       
'          en-US            M/d/yyyy   5/1/2011
'          ja-JP          yyyy/MM/dd 2011/05/01
'          fr-FR          dd/MM/yyyy 01/05/2011

下面的示例修改 ShortDatePattern 对象的 属性DateTimeFormatInfo,该对象表示英语 (美国) 区域性的格式设置约定。 它还显示日期值两次,首先反映原始 ShortDatePattern 属性,然后反映新的属性值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTimeFormatInfo dtfi = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat;
      DateTime date1 = new DateTime(2011, 5, 1);
      Console.WriteLine("Original Short Date Pattern:");
      Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern,
                                       date1.ToString("d", dtfi));
      dtfi.DateSeparator = "-";
      dtfi.ShortDatePattern = @"yyyy/MM/dd";
      Console.WriteLine("Revised Short Date Pattern:");
      Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern,
                                       date1.ToString("d", dtfi));
   }
}
// The example displays the following output:
//       Original Short Date Pattern:
//          M/d/yyyy: 5/1/2011
//       Revised Short Date Pattern:
//          yyyy/MM/dd: 2011-05-01
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim dtfi As DateTimeFormatInfo = CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat
      Dim date1 As DateTime = #05/01/2011#
      Console.WriteLine("Original Short Date Pattern:")
      Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern, 
                                       date1.ToString("d", dtfi))
      dtfi.DateSeparator = "-"
      dtfi.ShortDatePattern = "yyyy/MM/dd"
      Console.WriteLine("Revised Short Date Pattern:")
      Console.WriteLine("   {0}: {1}", dtfi.ShortDatePattern, 
                                       date1.ToString("d", dtfi))
   End Sub
End Module
' The example displays the following output:
'    Original Short Date Pattern:
'       M/d/yyyy: 5/1/2011
'    Revised Short Date Pattern:
'       YYYY/MM/dd: 2011-05-01

注解

属性 ShortDatePattern 定义通过调用 DateTime.ToStringDateTimeOffset.ToString 方法以及提供“d”标准格式字符串的复合格式字符串返回的日期字符串的区域性特定格式。

如果属性的值 Calendar 发生更改,则此属性将受到影响。

适用于

另请参阅