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
Long Date ("D") Format Specifier in C#
The "D" format specifier in C# represents the long date format pattern. It displays dates in a culture-specific long format, showing the full day name, month name, day, and year without time information.
The format string is defined by the culture's DateTimeFormatInfo.LongDatePattern property and varies based on the specified culture.
Syntax
Following is the syntax for using the "D" format specifier −
DateTime.ToString("D")
DateTime.ToString("D", CultureInfo)
The default custom format string for English (US) culture is −
dddd, dd MMMM yyyy
Using "D" Format with Default Culture
Example
using System;
class Demo {
static void Main() {
DateTime myDate = new DateTime(2018, 9, 20);
Console.WriteLine("Long Date Format: " + myDate.ToString("D"));
DateTime currentDate = DateTime.Now;
Console.WriteLine("Current Date: " + currentDate.ToString("D"));
}
}
The output of the above code is −
Long Date Format: Thursday, September 20, 2018 Current Date: Monday, December 23, 2024
Using "D" Format with Specific Cultures
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
DateTime myDate = new DateTime(2018, 9, 20);
Console.WriteLine("US English: " + myDate.ToString("D", CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine("French: " + myDate.ToString("D", CultureInfo.CreateSpecificCulture("fr-FR")));
Console.WriteLine("German: " + myDate.ToString("D", CultureInfo.CreateSpecificCulture("de-DE")));
Console.WriteLine("Spanish: " + myDate.ToString("D", CultureInfo.CreateSpecificCulture("es-ES")));
}
}
The output of the above code is −
US English: Thursday, September 20, 2018 French: jeudi 20 septembre 2018 German: Donnerstag, 20. September 2018 Spanish: jueves, 20 de septiembre de 2018
Comparison with Other Date Format Specifiers
| Format Specifier | Description | Example (en-US) |
|---|---|---|
| "D" | Long date pattern | Thursday, September 20, 2018 |
| "d" | Short date pattern | 9/20/2018 |
| "F" | Full date/time (long date and long time) | Thursday, September 20, 2018 12:00:00 AM |
| "M" | Month/day pattern | September 20 |
Getting the Long Date Pattern
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
CultureInfo usCulture = CultureInfo.CreateSpecificCulture("en-US");
CultureInfo frenchCulture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine("US Long Date Pattern: " + usCulture.DateTimeFormat.LongDatePattern);
Console.WriteLine("French Long Date Pattern: " + frenchCulture.DateTimeFormat.LongDatePattern);
DateTime date = new DateTime(2018, 9, 20);
Console.WriteLine("\nFormatted Dates:");
Console.WriteLine("US: " + date.ToString("D", usCulture));
Console.WriteLine("French: " + date.ToString("D", frenchCulture));
}
}
The output of the above code is −
US Long Date Pattern: dddd, MMMM d, yyyy French Long Date Pattern: dddd d MMMM yyyy Formatted Dates: US: Thursday, September 20, 2018 French: jeudi 20 septembre 2018
Conclusion
The "D" format specifier provides culture-aware long date formatting in C#. It automatically adapts to different cultures, displaying dates with full day and month names in the appropriate language and format. This makes it ideal for user-facing applications that need to display dates in a readable, localized format.
