How to convert date object to string in C#?


Here you will learn how to convert a DateTime object to a string in C#.

The DateTime struct includes the following methods that return date and time as a string.

Method Description
DateTime.ToString() Converts a DateTime value to a string in the specified format of the current culture.
DateTime.ToShortDateString() Converts a DateTime value to a short date string (M/d/yyyy pattern) in the current culture.
DateTime.ToShortTimeString() Converts a DateTime value to a short time string (h:mm:ss pattern) in the current culture.
DateTime.ToLongDateString() Converts a DateTime value to a long date string (dddd, MMMM d, yyyy pattern) in the current culture.
DateTime.ToLongTimeString() Converts a DateTime value to a long time string (h:mm:ss tt pattern) in the current culture.

Convert DateTime to String using the ToString() Method

Use the DateTime.ToString() method to convert the date object to string with the local culture format. The value of the DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. For example, the culture on your local/server environment is set to en-US, then you will get the string value of a date in MM/DD/YYYY format using any of the above methods.

The following converts the date portion of a DateTime object into a string.

Example: Convert Date to String
var todayDate = DateTime.Today;

string strToday = todayDate.ToString(); // converts date to string as per current culture
Console.WriteLine(strToday);

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
string strTodayUS = todayDate.ToString(); // converts date to string in MM/DD/YYYY format
Console.WriteLine(strTodayUS);

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
string strTodayFR = todayDate.ToString(); // converts date to string in DD/MM/YYYY format
Console.WriteLine(strTodayFR);

In the above example, the ToString() method converts a date to a string based on the DateTimeFormatInfo.ShortDatePattern property of the current thread culture by default.

Convert DateTime to String in Specific Format

You can specify the specific date and time formats in the ToString() method to get a date and time string into a particular format. The following example demonstrates getting date value as a string in different formats using the ToString() method.

Example: Convert DateTime to String in Specific Format
var dt = DateTime.Now;

Console.WriteLine("Date in Current Culture: " + dt.ToString("d"));
Console.WriteLine("MM/dd/yyyy Format: " + dt.ToString("MM/dd/yyyy")); //e.g. 06/18/2021
Console.WriteLine("dddd, dd MMMM yyyy Format: " + dt.ToString("dddd, dd MMMM yyyy")); //e.g. Friday, 18 June 2021
Console.WriteLine("MM/dd/yyyy h:mm tt Format: " + dt.ToString("MM/dd/yyyy h:mm tt")); //e.g. 06/18/2021 12:44 PM
Console.WriteLine("MMMM dd Format:" + dt.ToString("MMMM dd")); //e.g. June 18
Console.WriteLine("HH:mm:ss Format: " + dt.ToString("HH:mm:ss")); // e.g. 12:44:46
Console.WriteLine("hh:mm tt Format: " + dt.ToString("hh:mm tt")); // e.g. 12:44

Visit date and time format specifiers to know all the format specifiers that can be used with the ToString() method.

Convert DateTime to Date String

Use the ToShortDateString() or ToLongDateString() to get the string of date portion in a short or long format based on your local culture, as shown below.

Example: DateTime to Date String
var dt = DateTime.Now;

Console.WriteLine("Short Date String: " + dt.ToShortDateString()); // e.g. 06/18/2021
Console.WriteLine("Long Date String: " + dt.ToLongDateString());  // e.g. Friday, June 18, 2021

The ToShortDateString() method uses the ShortDatePattern and the ToLongDateString() method uses the LongDatePattern property property associated with the current thread culture.

Convert DateTime to Time String

Use the ToShortTimeString() or ToLongTimeString() to get the string of time portion in a short or long format based on your local culture, as shown below.

Example: DateTime to Time String
var dt = DateTime.Now;

Console.WriteLine("Short Time String: " + dt.ToShortTimeString()); //e.g. 12:10 
Console.WriteLine("Long Time String: " + dt.ToLongTimeString());   //e.g. 12:10:50

The ToShortTimeString() method uses the pattern defined by the ShortTimePattern property and the ToLongTimeString() method uses the LongTimePattern property associated with the current thread culture.

Conclusion

Use the ToString() method to convert a date object to different formats as per your need. Use ToShortDateString() or ToShortTimeString() to get short date and time string. Use ToLongDateString() or ToLongTimeString() to get the date and time in long format.