The DateTime structure represents dates and times, and it provides many properties and methods for formatting date and time values into strings and for parsing strings to date and time values. Understanding date and time formats is important for displaying date and time values in different cultures and for specific formatting needs.
Standard Date and Time Format Strings C# provides a set of standard date and time format strings that can be used with the ToString method of DateTime for quick formatting.
Here are some common standard format specifiers:
Pattern
Description
Example
"d"
Short date pattern
6/10/2024
"D"
Long date pattern
Monday, June 10, 2024
"f"
Full date/time pattern (short time)
Monday, June 10, 2024 10:45 AM
"F"
Full date/time pattern (long time)
Monday, June 10, 2024 10:45:30 AM
"g"
General date/time pattern (short time)
6/10/2024 10:45 AM
"G"
General date/time pattern (long time)
6/10/2024 10:45:30 AM
"t"
Short time pattern
10:45 AM
"T"
Long time pattern
10:45:30 AM
"M" or "m"
Month day pattern
June 10
"Y" or "y"
Year month pattern
June 2024
"O" or "o"
Round-trip date/time pattern
2024-06-10T10:45:30.0000000
Custom Date and Time Format Strings For more control over the formatting of DateTime values, you can use custom date and time format strings.
Here are some common custom format specifiers:
Pattern
Description
Example
"dd"
Day of the month (01-31)
01
"ddd"
Abbreviated day of the week
Mon
"dddd"
Full day of the week
Monday
"MM"
Month number (01-12)
06
"MMM"
Abbreviated month name
Jun
"MMMM"
Full month name
June
"yy"
Two-digit year
24
"yyyy"
Four-digit year
2024
"HH"
Hour (00-23)
13
"hh"
Hour (01-12)
01
"mm"
Minutes (00-59)
45
"ss"
Seconds (00-59)
30
"fff"
Milliseconds (000-999)
123
"tt"
AM/PM designator
AM
Example of Standard and Custom date Formatting
using System;
namespace ConsoleApplication1
{
class ShowDates
{
public static void Main(string[] args)
{
DateTime now = DateTime.Now;
// Standard date and time format strings
Console.WriteLine("Standard Formats:");
Console.WriteLine(now.ToString("d")); // Short date
Console.WriteLine(now.ToString("D")); // Long date
Console.WriteLine(now.ToString("f")); // Full date/time (short time)
Console.WriteLine(now.ToString("F")); // Full date/time (long time)
Console.WriteLine(now.ToString("g")); // General date/time (short time)
Console.WriteLine(now.ToString("G")); // General date/time (long time)
Console.WriteLine(now.ToString("t")); // Short time
Console.WriteLine(now.ToString("T")); // Long time
Console.WriteLine(now.ToString("M")); // Month day
Console.WriteLine(now.ToString("y")); // Year month
Console.WriteLine(now.ToString("O")); // Round-trip date/time
// Custom date and time format strings
Console.WriteLine("\nCustom Formats:");
Console.WriteLine(now.ToString("dd-MM-yyyy")); // Day-Month-Year
Console.WriteLine(now.ToString("dddd, MMMM dd yyyy")); // Full day, Month day year
Console.WriteLine(now.ToString("MM/dd/yyyy HH:mm")); // Month/Day/Year Hour:Minute
Console.WriteLine(now.ToString("hh:mm:ss tt")); // Hour:Minute:Second AM/PM
Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm:ss")); // Year-Month-Day Hour:Minute:Second
}
}
}
Output -
Culture-Specific Formatting
Date and time formats can vary by culture. You can specify a culture when formatting or parsing dates to ensure the correct format for different locales.
using System;
using System.Globalization;
namespace ConsoleApplication1
{
class ShowDates
{
public static void Show()
{
DateTime now = DateTime.Now;
// Array of 10 different culture names
string[] cultureNames = new string[]
{
"en-US", // English (United States)
"fr-FR", // French (France)
"de-DE", // German (Germany)
"es-ES", // Spanish (Spain)
"ja-JP", // Japanese (Japan)
"ru-RU", // Russian (Russia)
"it-IT", // Italian (Italy)
"pt-BR", // Portuguese (Brazil)
};
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Standard Formats:\n\n");
Console.ResetColor();
CultureInfo culture = null;
// Print the culture names and their display names
foreach (var cultureName in cultureNames)
{
culture = new CultureInfo(cultureName);
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("Culture Name: {0}, Display Name: {1}", cultureName, culture.DisplayName);
Console.ResetColor();
Console.WriteLine(now.ToString("d", culture)); // Short date
Console.WriteLine(now.ToString("D", culture)); // Long date
Console.WriteLine(now.ToString("f", culture)); // Full date/time (short time)
Console.WriteLine("------------------------------------------------------------------------");
}
}
}
}
Output :
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The DateTime structure represents dates and times, and it provides many properties and methods for formatting date and time values into strings and for parsing strings to date and time values. Understanding date and time formats is important for displaying date and time values in different cultures and for specific formatting needs.
Standard Date and Time Format Strings
C# provides a set of standard date and time format strings that can be used with the ToString method of DateTime for quick formatting.
Here are some common standard format specifiers:
Custom Date and Time Format Strings
For more control over the formatting of DateTime values, you can use custom date and time format strings.
Here are some common custom format specifiers:
Example of Standard and Custom date Formatting
Output -
Culture-Specific Formatting
Date and time formats can vary by culture. You can specify a culture when formatting or parsing dates to ensure the correct format for different locales.
Output :