C# How to Convert String to DateTime Easily
Use
DateTime.Parse(string) to convert a string to DateTime directly, or use DateTime.TryParse(string, out DateTime) to safely convert with error checking.Examples
Input"2024-06-01"
OutputDateTime object representing June 1, 2024
Input"06/01/2024 14:30"
OutputDateTime object representing June 1, 2024 at 2:30 PM
Input"invalid date"
OutputTryParse returns false, no DateTime created
How to Think About It
To convert a string to a DateTime, first decide if you want to handle invalid formats safely. Use
DateTime.Parse if you trust the string format, or DateTime.TryParse to check if conversion works without errors.Algorithm
1
Get the input string representing a date and/or time.2
Try to convert the string to a DateTime using a parsing method.3
If using TryParse, check if conversion succeeded.4
Return the DateTime object if successful, or handle failure if not.Code
csharp
using System; class Program { static void Main() { string dateString = "2024-06-01 14:30"; if (DateTime.TryParse(dateString, out DateTime dateValue)) { Console.WriteLine($"Converted date: {dateValue}"); } else { Console.WriteLine("Invalid date format."); } } }
Output
Converted date: 6/1/2024 2:30:00 PM
Dry Run
Let's trace converting "2024-06-01 14:30" using TryParse.
1
Input string
dateString = "2024-06-01 14:30"
2
Try to parse
DateTime.TryParse("2024-06-01 14:30", out dateValue) returns true
3
Output result
dateValue = 6/1/2024 2:30:00 PM
| Step | Action | Result |
|---|---|---|
| 1 | Input string | "2024-06-01 14:30" |
| 2 | TryParse conversion | Success, dateValue set |
| 3 | Print dateValue | 6/1/2024 2:30:00 PM |
Why This Works
Step 1: Parsing the string
The DateTime.TryParse method attempts to read the string and convert it into a date and time.
Step 2: Checking success
TryParse returns true if the string is a valid date/time format, otherwise false.
Step 3: Using the result
If successful, the parsed date/time is stored in the output variable for further use.
Alternative Approaches
DateTime.Parse
csharp
using System; class Program { static void Main() { string dateString = "2024-06-01 14:30"; DateTime dateValue = DateTime.Parse(dateString); Console.WriteLine($"Converted date: {dateValue}"); } }
Throws an exception if the string is invalid, so use only when sure of format.
DateTime.ParseExact
csharp
using System; using System.Globalization; class Program { static void Main() { string dateString = "01-06-2024"; DateTime dateValue = DateTime.ParseExact(dateString, "dd-MM-yyyy", CultureInfo.InvariantCulture); Console.WriteLine($"Converted date: {dateValue}"); } }
Use when you know the exact format of the date string for precise parsing.
Complexity: O(1) time, O(1) space
Time Complexity
Parsing a string to DateTime is a fixed-time operation, so it runs in constant time.
Space Complexity
Only a fixed amount of memory is used for the DateTime object and temporary parsing.
Which Approach is Fastest?
All methods run in constant time; TryParse is safer but slightly slower due to error checking.
| Approach | Time | Space | Best For |
|---|---|---|---|
| DateTime.Parse | O(1) | O(1) | When input format is guaranteed valid |
| DateTime.TryParse | O(1) | O(1) | When input may be invalid, safer |
| DateTime.ParseExact | O(1) | O(1) | When exact format is known and required |
Use
DateTime.TryParse to avoid exceptions when converting unknown string formats.Using
DateTime.Parse without handling exceptions can crash your program if the string is invalid.