Hướng dẫn dùng datetime.now.toshortdatestring JavaScript - use JavaScript datetime.now.toshortdatestring

Bỏ qua nội dung chính

Trình duyệt này không còn được hỗ trợ.

Nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, cập nhật bảo mật và hỗ trợ kỹ thuật.

Phương thức datetime.toshortdatestring

  • Tài liệu tham khảo

Sự định nghĩa

Trong bài viết này

Chuyển đổi giá trị của đối tượng DateTime hiện tại thành biểu diễn chuỗi ngày ngắn tương đương của nó.

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

Trả lại

Sợi dây

Một chuỗi chứa biểu diễn chuỗi ngắn ngày của đối tượng DateTime hiện tại.

Ví dụ

Ví dụ sau đây cho thấy phương pháp ToshortDatestring. Nó cũng cho thấy kết quả của việc gọi phương thức ToshortDatestring giống hệt với việc gọi phương thức DateTime.ToString (Chuỗi) với "D" là tham số định dạng.

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

Nhận xét

Giá trị của đối tượng DateTime hiện tại được định dạng bằng cách sử dụng mẫu được xác định bởi thuộc tính datetimeformatinfo.shortdatepotype được liên kết với văn hóa hiện tại. Giá trị trả về giống hệt với giá trị được trả về bằng cách chỉ định chuỗi định dạng DateTime tiêu chuẩn "D" với phương thức TOSTRING (Chuỗi).

Ghi chú

Chuỗi được trả về bởi phương pháp Toshortdatestring là nhạy cảm với văn hóa. Nó phản ánh mẫu được xác định bởi thuộc tính datetimeformatinfo.shortdatepotyp của văn hóa văn hóa hiện tại. Ví dụ, đối với văn hóa en-us, mẫu ngày ngắn tiêu chuẩn là "m/d/yyyy"; Đối với văn hóa de-de, đó là "dd.mm.yyy"; Đối với văn hóa JA-JP, đó là "yyyy/mm/dd". Lưu ý rằng giá trị của nó có thể thay đổi tùy thuộc vào việc triển khai .NET và phiên bản của nó, hệ điều hành và phiên bản và tùy chỉnh người dùng.

Để biết thêm thông tin về văn hóa chủ đề hiện tại, hãy xem các thuộc tính văn hóa văn hóa. Để biết thêm thông tin về các ký tự định dạng, mẫu định dạng và đầu ra chúng tạo ra, xem chuỗi định dạng ngày và thời gian tiêu chuẩn và chuỗi định dạng ngày và thời gian tùy chỉnh. Để biết thêm thông tin về việc thay đổi mẫu định dạng được liên kết với ký tự định dạng, hãy xem lớp DateTimeFormatInfo.

Áp dụng cho

Xem thêm

  • Văn hóa
  • DateTimeFormat
  • Datetimeformatinfo
  • ToString()
  • Các loại định dạng trong .NET
  • Chuỗi định dạng ngày và thời gian tùy chỉnh