0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert DateTime to String Easily

In C#, convert a DateTime to a string using dateTimeVariable.ToString() or with a format like dateTimeVariable.ToString("yyyy-MM-dd") to control the output format.
📋

Examples

InputDateTime date = new DateTime(2024, 6, 15, 14, 30, 0); date.ToString()
Output6/15/2024 2:30:00 PM
InputDateTime date = new DateTime(2024, 6, 15); date.ToString("yyyy-MM-dd")
Output2024-06-15
InputDateTime date = DateTime.MinValue; date.ToString("O")
Output0001-01-01T00:00:00.0000000
🧠

How to Think About It

To convert a date and time to a string, think about how you want it to look. You can use the default string form or choose a pattern like year-month-day. Use the ToString() method on the date object, optionally giving it a format string to get the exact style you want.
📐

Algorithm

1
Get the DateTime value you want to convert.
2
Decide if you want the default string or a custom format.
3
Call the <code>ToString()</code> method on the DateTime object.
4
If needed, pass a format string to <code>ToString()</code> to control the output.
5
Return or print the resulting string.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        DateTime date = new DateTime(2024, 6, 15, 14, 30, 0);
        string defaultString = date.ToString();
        string formattedString = date.ToString("yyyy-MM-dd HH:mm");
        Console.WriteLine(defaultString);
        Console.WriteLine(formattedString);
    }
}
Output
6/15/2024 2:30:00 PM 2024-06-15 14:30
🔍

Dry Run

Let's trace converting a DateTime to string with a format.

1

Create DateTime

DateTime date = 2024-06-15 14:30:00

2

Call ToString() with format

date.ToString("yyyy-MM-dd HH:mm") returns "2024-06-15 14:30"

StepActionResult
1Create DateTime2024-06-15 14:30:00
2Convert to string with format"2024-06-15 14:30"
💡

Why This Works

Step 1: Use ToString() method

The ToString() method on a DateTime object converts it to a string representation.

Step 2: Apply format string

Passing a format string like "yyyy-MM-dd" tells ToString() how to arrange the date parts.

Step 3: Get formatted string

The method returns a string showing the date and time in the chosen format.

🔄

Alternative Approaches

Using string interpolation with ToString()
csharp
DateTime date = DateTime.Now;
string result = $"{date:yyyy-MM-dd HH:mm:ss}";
Console.WriteLine(result);
This uses C# string interpolation to format the date inline, which is concise and readable.
Using CultureInfo for localization
csharp
using System.Globalization;
DateTime date = DateTime.Now;
string result = date.ToString(CultureInfo.InvariantCulture);
Console.WriteLine(result);
This converts DateTime to string using a specific culture, useful for consistent formatting across regions.

Complexity: O(1) time, O(1) space

Time Complexity

Converting DateTime to string is a fixed-time operation with no loops, so it runs in constant time.

Space Complexity

The operation uses a small fixed amount of memory to create the output string.

Which Approach is Fastest?

Using ToString() directly is fastest; string interpolation adds minimal overhead but improves readability.

ApproachTimeSpaceBest For
ToString() with formatO(1)O(1)Simple, precise formatting
String interpolationO(1)O(1)Readable inline formatting
CultureInfo formattingO(1)O(1)Localization and culture-specific formats
💡
Use ToString() with a format string to get exactly the date format you want.
⚠️
Forgetting to use a format string and getting unexpected default date formats.