Bagikan melalui


DateTime.ToShortDateString Metode

Definisi

Mengonversi nilai objek saat ini DateTime ke representasi string tanggal pendek yang setara.

public:
 System::String ^ ToShortDateString();
public string ToShortDateString ();
member this.ToShortDateString : unit -> string
Public Function ToShortDateString () As String

Mengembalikan

String yang berisi representasi string tanggal pendek dari objek saat ini DateTime .

Contoh

Contoh berikut menunjukkan ToShortDateString metode . Ini juga menunjukkan bahwa hasil pemanggilan ToShortDateString metode identik dengan memanggil DateTime.ToString(String) metode dengan "d" sebagai parameter format.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime dateToDisplay = new DateTime(2009, 6, 1, 8, 42, 50);
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      // Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      Console.WriteLine("Displaying short date for {0} culture:",
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0} (Short Date String)",
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate it is
      // identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)",
                        dateToDisplay.ToString("d"));
      Console.WriteLine();

      // Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      Console.WriteLine("Displaying short date for {0} culture:",
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      Console.WriteLine();

      // Change culture to nl-NL.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
      Console.WriteLine("Displaying short date for {0} culture:",
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());

      // Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}
// The example displays the following output:
//       Displaying short date for en-US culture:
//          6/1/2009 (Short Date String)
//          6/1/2009 ('d' standard format specifier)
//
//       Displaying short date for fr-FR culture:
//          01/06/2009
//
//       Displaying short date for nl-NL culture:
//          1-6-2009
open System
open System.Globalization
open System.Threading

let dateToDisplay = DateTime(2009, 6, 1, 8, 42, 50)
let originalCulture = Thread.CurrentThread.CurrentCulture
// Change culture to en-US.
Thread.CurrentThread.CurrentCulture <- CultureInfo "en-US"
printfn "Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $"   {dateToDisplay.ToShortDateString()} (Short Date String)"

// Display using 'd' standard format specifier to illustrate it is
// identical to the string returned by ToShortDateString.
printfn $"   {dateToDisplay:d} ('d' standard format specifier)\n"

// Change culture to fr-FR.
Thread.CurrentThread.CurrentCulture <- CultureInfo "fr-FR"
printfn $"Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $"   {dateToDisplay.ToShortDateString()}\n"

// Change culture to nl-NL.
Thread.CurrentThread.CurrentCulture <- CultureInfo "nl-NL"
printfn $"Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $"   {dateToDisplay.ToShortDateString()}"

// Restore original culture.
Thread.CurrentThread.CurrentCulture <- originalCulture


// The example displays the following output:
//       Displaying short date for en-US culture:
//          6/1/2009 (Short Date String)
//          6/1/2009 ('d' standard format specifier)
//
//       Displaying short date for fr-FR culture:
//          01/06/2009
//
//       Displaying short date for nl-NL culture:
//          1-6-2009
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim dateToDisplay As Date = #06/01/2009 8:42:50#
      Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      ' Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0} (Short Date String)", _
                        dateToDisplay.ToShortDateString())
      ' Display using 'd' standard format specifier to illustrate it is
      ' identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", _
                        dateToDisplay.ToString("d"))
      Console.WriteLine()
      
      ' Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString())
      Console.WriteLine()
  
      ' Change culture to nl-NL.    
      Thread.CurrentThread.CurrentCulture = New CultureInfo("nl-NL")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString())
      
      ' Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture
   End Sub
End Module
' The example displays the following output:
'       Displaying short date for en-US culture:
'          6/1/2009 (Short Date String)
'          6/1/2009 ('d' standard format specifier)
'       
'       Displaying short date for fr-FR culture:
'          01/06/2009
'       
'       Displaying short date for nl-NL culture:
'          1-6-2009

Keterangan

Nilai objek saat ini DateTime diformat menggunakan pola yang ditentukan oleh properti yang DateTimeFormatInfo.ShortDatePattern terkait dengan budaya saat ini. Nilai yang dikembalikan identik dengan nilai yang dikembalikan dengan menentukan string format DateTime standar "d" dengan ToString(String) metode .

Catatan

String yang ToShortDateString dikembalikan oleh metode sensitif terhadap budaya. Ini mencerminkan pola yang ditentukan oleh properti budaya DateTimeFormatInfo.ShortDatePattern saat ini. Misalnya, untuk budaya en-US, pola tanggal pendek standar adalah "M/d/yyyy"; untuk budaya de-DE, itu adalah "dd.MM.yyyy"; untuk budaya ja-JP, itu adalah "yyyy/MM/dd". Perhatikan bahwa nilainya dapat bervariasi tergantung pada implementasi .NET dan versinya, sistem operasi dan versinya, serta penyesuaian pengguna.

Untuk informasi selengkapnya tentang budaya utas saat ini, lihat CultureInfo.CurrentCulture properti dan Thread.CurrentCulture . Untuk informasi selengkapnya tentang format karakter, pola format, dan output yang dihasilkannya, lihat String format tanggal dan waktu standar serta String format tanggal dan waktu kustom. Untuk informasi selengkapnya tentang mengubah pola format yang terkait dengan karakter format, lihat DateTimeFormatInfo kelas .

Berlaku untuk

Lihat juga