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.
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.
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.
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.
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.
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- Compare strings in C#
- How to count elements in C# array?
- Difference between String and string in C#.
- How to get a comma separated string from an array in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?
- How to calculate the code execution time in C#?