Aracılığıyla paylaş


DateTimeFormatInfo.ShortDatePattern Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: December 2010

Gets or sets the custom date and time format string for a short date value, which is associated with the "d" standard format string.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Property ShortDatePattern As String
public string ShortDatePattern { get; set; }

Property Value

Type: System.String
The custom format string for a short date value, which is associated with the "d" standard format string.

Exceptions

Exception Condition
ArgumentNullException

An attempt was made to set the property to nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The DateTimeFormatInfo object is read-only.

Remarks

See Custom Date and Time Format Strings for custom date and time format specifiers that can be combined to construct custom format strings such as "MM/dd/yyyy".

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

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.

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.FontFamily = New System.Windows.Media.FontFamily("Courier New")
      Dim cultures() As String = { "en-US", "ja-JP", "fr-FR" }
      Dim date1 As Date = #05/01/2011#

      outputBlock.Text += String.Format(" {0,7} {1,19} {2,10}", 
                                        "CULTURE", "PROPERTY VALUE", "DATE") 
      outputBlock.Text += vbCrLf + vbCrLf

      For Each culture As String In cultures
         Dim dtfi As DateTimeFormatInfo = (New CultureInfo(culture)).DateTimeFormat
         outputBlock.Text += String.Format(" {0,7} {1,19} {2,10}", culture, 
                                           dtfi.ShortDatePattern, 
                                           date1.ToString("d", dtfi)) + vbCrLf
      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
using System;
using System.Globalization;

public class Example  
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)  
   {
      outputBlock.FontFamily = new System.Windows.Media.FontFamily("Courier New");
      string[]  cultures = { "en-US", "ja-JP", "fr-FR" };
      DateTime date1 = new DateTime(2011, 5, 1);

      outputBlock.Text += String.Format(" {0,7} {1,19} {2,10}\n\n", 
                                        "CULTURE", "PROPERTY VALUE", "DATE");

      foreach (var culture in cultures) {
         DateTimeFormatInfo dtfi = (new CultureInfo(culture)).DateTimeFormat;
         outputBlock.Text += String.Format(" {0,7} {1,19} {2,10}\n",
                                           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.

Imports System.Globalization

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dtfi As DateTimeFormatInfo = (New CultureInfo("en-US")).DateTimeFormat
      Dim date1 As DateTime = #5/1/2011#
      outputBlock.Text &= "Original Short Date Pattern:" & vbCrLf
      outputBlock.Text += String.Format("   {0}: {1}", dtfi.ShortDatePattern,  
                                       date1.ToString("d", dtfi)) & vbCrLf
      dtfi.ShortDatePattern = "yyyy/MM/dd"
      outputBlock.Text &= "Revised Short Date Pattern:" & vbCrLf
      outputBlock.Text += String.Format("   {0}: {1}", dtfi.ShortDatePattern,  
                                       date1.ToString("d", dtfi)) & vbCrLf
   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
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      DateTimeFormatInfo dtfi = (new CultureInfo("en-US")).DateTimeFormat;
      DateTime date1 = new DateTime(2011, 5, 1);
      outputBlock.Text += "Original Short Date Pattern:" + "\n";
      outputBlock.Text += String.Format("   {0}: {1}", dtfi.ShortDatePattern,
                                       date1.ToString("d", dtfi)) + "\n";
      dtfi.ShortDatePattern = @"yyyy/MM/dd";
      outputBlock.Text += "Revised Short Date Pattern:" + "\n";
      outputBlock.Text += String.Format("   {0}: {1}", dtfi.ShortDatePattern,
                                       date1.ToString("d", dtfi)) + "\n";
   }
}
// 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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

December 2010

Revised the first example and added the second example.

Customer feedback.