What is Date Time Format With Example Code

DateTime.Now retrieves the current date and time.
Standard Date and Time Format Specifiers are used with the ToString() method to format the DateTime value according to predefined patterns.
Custom Date and Time Format Specifiers allow specifying custom format patterns for date and time representations.




using System;
 
class Program
{
    static void Main(string[] args)
    {
        DateTime now = DateTime.Now;
 
        // Standard Date and Time Format Specifiers
        Console.WriteLine($"Short Date String: {now.ToString("d")}"); 
        // Short date pattern. Represents a combination of short date and short time (for cultures that do not use the short time format, the value is the same as 'D').
        Console.WriteLine($"Long Date String: {now.ToString("D")}");  
        // Long date pattern. Represents a combination of long date and long time.
        Console.WriteLine($"Short Time String: {now.ToString("t")}");  
        // Short time pattern. Represents the short time pattern (for cultures that do not use the short time format, the value is the same as 'T').
        Console.WriteLine($"Long Time String: {now.ToString("T")}");   
        // Long time pattern. Represents the long time pattern.
        Console.WriteLine($"General Date/Time String: {now.ToString("f")}"); 
        // Full date and time (short time).
        Console.WriteLine($"Universal Date/Time String: {now.ToString("u")}");
        // Full date and time (long time).
        Console.WriteLine($"Sortable Date/Time String: {now.ToString("s")}"); 
        // Sortable date and time pattern. The date and time are represented in the format 'yyyy-MM-ddTHH:mm:ss'.
        Console.WriteLine($"Round-trip Date/Time String: {now.ToString("o")}");
        // Round-trip date and time pattern. Represents a custom date and time format string using a pattern that preserves time zone information.
 
        // Custom Date and Time Format Specifiers
        Console.WriteLine($"Custom Date Format (dd/MM/yyyy): {now.ToString("dd/MM/yyyy")}");
        // Custom date format (day/month/year).
        Console.WriteLine($"Custom Time Format (HH:mm:ss): {now.ToString("HH:mm:ss")}");  
        // Custom time format (24-hour clock).
        Console.WriteLine($"Custom Date and Time Format (yyyy-MM-dd HH:mm:ss): {now.ToString("yyyy-MM-dd HH:mm:ss")}"); 
        // Custom date and time format (year-month-day hour:minute:second).
    }
}
 

//Please Check

//Result

//Short Date String: 3 / 24 / 2024

//Long Date String: Sunday, March 24, 2024

//Short Time String: 4:09 PM

//Long Time String: 4:09:05 PM

//General Date/Time String: Sunday, March 24, 2024 4:09 PM

//Universal Date/Time String: 2024 - 03 - 24 16:09:05Z

//Sortable Date/Time String: 2024 - 03 - 24T16: 09:05

//Round - trip Date / Time String: 2024 - 03 - 24T16: 09:05.1910000 + 00:00

//Custom Date Format (dd/MM/yyyy): 24 / 03 / 2024

//Custom Time Format (HH:mm: ss): 16:09:05

//Custom Date and Time Format (yyyy-MM-dd HH:mm: ss): 2024 - 03 - 24 16:09:05