Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ตัวระบุรูปแบบวันที่ทั่วไปเวลายาว (G) ใน C#

ตัวระบุรูปแบบมาตรฐาน "G" (General Date Long Time) รวมรูปแบบวันที่สั้น (Short Date "d") และเวลายาว (Long Time "T") คั่นด้วยช่องว่าง

ตัวอย่างโค้ด

using System;
using System.Globalization;

class Demo
{
    static void Main()
    {
        DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);
        Console.WriteLine(dt.ToString("G", DateTimeFormatInfo.InvariantInfo));
    }
}

ผลลัพธ์

01/03/2018 03:45:20

InvariantInfo: ใช้วัฒนธรรมที่ไม่เปลี่ยนแปลง (Invariant Culture) เพื่อให้รูปแบบคงที่ ไม่ขึ้นกับการตั้งค่าระบบ