DateTimeFormatInfo.ShortDatePattern Property

Definition

Gets or sets the custom format string for a short date value.

public string ShortDatePattern { get; set; }

Property Value

The custom format string for a short date value.

Exceptions

The property is being set to null.

The property is being set and the DateTimeFormatInfo object is read-only.

Examples

The following example displays the value of the ShortDatePattern property and the value of a date formatted using the ShortDatePattern property for a few cultures.

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

The following example modifies the ShortDatePattern property of a DateTimeFormatInfo object that represents the formatting conventions of the English (United States) culture. It also displays a date value twice, first to reflect the original ShortDatePattern property and then to reflect the new property value.

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

Remarks

The ShortDatePattern property defines the culture-specific format of date strings that are returned by calls to the DateTime.ToString and DateTimeOffset.ToString methods and by composite format strings that are supplied the "d" standard format string.

This property is affected if the value of the Calendar property changes.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also