DateTime.Parse() / DateTime.TryParse()
Converts a string representing a date/time to a DateTime value using DateTime.Parse(), or use the safer DateTime.TryParse() which does not throw an exception on failure.
Syntax
// Converts a string to DateTime (throws an exception on failure). DateTime dt = DateTime.Parse(string s); // Attempts conversion and returns true on success or false on failure (no exception). bool success = DateTime.TryParse(string s, out DateTime result); // Converts using a specified format. DateTime dt = DateTime.ParseExact(string s, string format, IFormatProvider provider); bool success = DateTime.TryParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result);
Method list
| Method | Description |
|---|---|
| DateTime.Parse(s) | Converts a string to DateTime. Throws a FormatException if the conversion fails. |
| DateTime.TryParse(s, out result) | Returns true and stores the converted value in result on success. Returns false on failure without throwing an exception. |
| DateTime.ParseExact(s, format, provider) | Converts only when the string exactly matches the specified format. Use this when a strict format is required. |
| DateTime.TryParseExact(s, format, provider, style, out result) | The exception-free version of ParseExact. |
Sample code
using System;
using System.Globalization;
// Convert using DateTime.Parse().
DateTime dt1 = DateTime.Parse("2024/01/15");
Console.WriteLine(dt1); // 2024/01/15 0:00:00
DateTime dt2 = DateTime.Parse("2024-03-20 14:30:00");
Console.WriteLine(dt2); // 2024/03/20 14:30:00
// Convert safely using TryParse() (recommended for user input).
string input = "2024/06/01";
if (DateTime.TryParse(input, out DateTime result)) {
Console.WriteLine($"Converted: {result:yyyy/M/d}");
} else {
Console.WriteLine("Conversion failed.");
}
// A string that cannot be converted returns false.
bool success = DateTime.TryParse("abc", out DateTime failed);
Console.WriteLine($"Success: {success}"); // False
// Specify a strict format using ParseExact().
DateTime dt3 = DateTime.ParseExact("20240115", "yyyyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine(dt3); // 2024/01/15 0:00:00
// Example using TryParseExact().
bool ok = DateTime.TryParseExact(
"15-01-2024",
"dd-MM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTime dt4
);
Console.WriteLine($"Success: {ok}, Result: {dt4:yyyy/MM/dd}");
Notes
When converting strings from user input or files to DateTime, use TryParse() or TryParseExact(), which do not throw exceptions. Use Parse() only for strings with a guaranteed format, such as constants or database values.
When the format is fixed to a specific pattern (such as filenames in yyyyMMdd format), ParseExact() is convenient. Specifying CultureInfo.InvariantCulture ensures locale-independent conversion.
For formatting a DateTime value after conversion, see DateTime.ToString() / DateTime.AddDays().
If you find any errors or copyright issues, please contact us.