Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Short Date ("d") Format Specifier
The "d" format specifier in C# represents the short date pattern for formatting DateTime values. This standard format specifier produces a compact date representation without time information, making it ideal for displaying dates in a concise format.
The format string is defined by the culture's DateTimeFormatInfo.ShortDatePattern property, which varies based on the current culture settings.
Syntax
Following is the syntax for using the "d" format specifier −
DateTime.ToString("d")
DateTime.ToString("d", CultureInfo)
The default custom format string for invariant culture is −
MM/dd/yyyy
Using "d" Format Specifier with Different Cultures
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
DateTime myDate = new DateTime(2018, 9, 09);
Console.WriteLine(myDate.ToString("d", DateTimeFormatInfo.InvariantInfo));
Console.WriteLine(myDate.ToString("d", CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(myDate.ToString("d", CultureInfo.CreateSpecificCulture("en-GB")));
Console.WriteLine(myDate.ToString("d", CultureInfo.CreateSpecificCulture("de-DE")));
}
}
The output of the above code is −
09/09/2018 9/9/2018 09/09/2018 09.09.2018
Current Culture vs Specific Culture
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
DateTime currentDate = DateTime.Now;
// Using current system culture
Console.WriteLine("Current culture: " + currentDate.ToString("d"));
// Using specific cultures
Console.WriteLine("US format: " + currentDate.ToString("d", new CultureInfo("en-US")));
Console.WriteLine("French format: " + currentDate.ToString("d", new CultureInfo("fr-FR")));
Console.WriteLine("Japanese format: " + currentDate.ToString("d", new CultureInfo("ja-JP")));
}
}
The output of the above code is −
Current culture: 12/30/2024 US format: 12/30/2024 French format: 30/12/2024 Japanese format: 2024/12/30
Comparison with Other Date Format Specifiers
| Format Specifier | Description | Example Output (en-US) |
|---|---|---|
| "d" | Short date pattern | 9/9/2018 |
| "D" | Long date pattern | Sunday, September 9, 2018 |
| "M" or "m" | Month day pattern | September 9 |
| "Y" or "y" | Year month pattern | September 2018 |
Conclusion
The "d" format specifier provides a culture-sensitive way to display short date formats in C#. It automatically adapts to different cultural conventions, making it essential for applications that need to display dates in various regional formats without explicitly defining custom format strings.
