Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Định dạng ngày Short Date (d) trong C#

Chuẩn "d" (Short Date) hiển thị ngày tháng năm ngắn gọn. Mẫu: MM/dd/yyyy (InvariantCulture) hoặc theo culture cụ thể.

Ví dụ minh họa

using System;
using System.Globalization;

class Demo
{
    static void Main()
    {
        DateTime myDate = new DateTime(2018, 9, 9);
        Console.WriteLine(myDate.ToString("d", DateTimeFormatInfo.InvariantInfo));
        Console.WriteLine(myDate.ToString("d", CultureInfo.CreateSpecificCulture("en-US")));
    }
}

Kết quả đầu ra

09/09/2018
9/9/2018

Phân biệt:

  • "d" (lowercase): Short Date pattern
  • "D" (uppercase): Long Date pattern
  • "g": General Date Short Time (Short Date + Short Time)
  • "G": General Date Long Time (Short Date + Long Time)