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
DateTime.ToLongDateString() Method in C#
The DateTime.ToLongDateString() method in C# converts a DateTime object to its equivalent long date string representation. This method returns a human-readable date format that includes the day of the week, month name, day, and year, formatted according to the current culture settings.
Syntax
Following is the syntax −
public string ToLongDateString();
Return Value
This method returns a string that represents the long date format of the DateTime object. The format follows the current culture's long date pattern, typically in the form "Day, Month Date, Year".
Using ToLongDateString() with Specific DateTime
The following example demonstrates converting a specific DateTime to a long date string −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 11, 9, 10, 45);
Console.WriteLine("Date = {0}", d);
string str = d.ToLongDateString();
Console.WriteLine("Long date string representation = {0}", str);
}
}
The output of the above code is −
Date = 10/11/2019 9:10:45 AM Long date string representation = Friday, October 11, 2019
Using ToLongDateString() with Current DateTime and Culture
This example shows how the method works with the current date and displays the culture-specific pattern −
using System;
using System.Globalization;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 16, 8, 39, 8);
Console.WriteLine("Date = {0}", d);
Console.WriteLine("Current culture = " + CultureInfo.CurrentCulture.Name);
var pattern = CultureInfo.CurrentCulture.DateTimeFormat;
string str = d.ToLongDateString();
Console.WriteLine("Long date pattern = {0}", pattern.LongDatePattern);
Console.WriteLine("Long date string representation = {0}", str);
}
}
The output of the above code is −
Date = 10/16/2019 8:39:08 AM Current culture = en-US Long date pattern = dddd, MMMM d, yyyy Long date string representation = Wednesday, October 16, 2019
Multiple DateTime Examples
The following example demonstrates the method with different dates −
using System;
public class Demo {
public static void Main() {
DateTime[] dates = {
new DateTime(2023, 1, 1),
new DateTime(2023, 7, 4),
new DateTime(2023, 12, 25)
};
foreach (DateTime date in dates) {
Console.WriteLine("Short: {0} | Long: {1}",
date.ToShortDateString(),
date.ToLongDateString());
}
}
}
The output of the above code is −
Short: 1/1/2023 | Long: Sunday, January 1, 2023 Short: 7/4/2023 | Long: Tuesday, July 4, 2023 Short: 12/25/2023 | Long: Monday, December 25, 2023
Key Features
-
The method uses the current thread's culture settings to format the date.
-
It includes the full day name, month name, day number, and year.
-
The time portion of the
DateTimeis ignored in the output. -
The format can vary based on the system's regional settings.
Conclusion
The DateTime.ToLongDateString() method provides an easy way to convert DateTime objects into user-friendly, culture-aware long date strings. It's particularly useful for displaying dates in a readable format that includes the day of the week and full month names.
