DateTimeFormatInfo.ShortDatePattern 屬性

定義

取得或設定簡短日期值的自訂格式字串。

public string ShortDatePattern { get; set; }

屬性值

簡短日期值的自訂格式字串。

例外狀況

此屬性設定為 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

下列範例會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

備註

屬性 ShortDatePattern 會定義呼叫 DateTime.ToStringDateTimeOffset.ToString 方法以及提供「d」 標準格式字串的複合格式字串所傳回之日期字串的文化特性特定格式。

如果屬性的值變更, Calendar 這個屬性就會受到影響。

適用於

另請參閱