In C#, DateTime.TryParse tries to read date text. It returns true if the text is valid and puts the result in an out parameter. If the text is invalid it returns false and the result is DateTime.MinValue.
Use it for text from users, files, or the web, because bad input is normal and should not crash the program.
This is a static member of System.DateTime. You call it with the type name, such as DateTime.TryParse(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.
Syntax
Pass the text and an out DateTime variable. Add a culture when needed.
C#
bool ok1 = DateTime.TryParse("2026-11-18", out DateTime d1);
bool ok2 = DateTime.TryParse("18/11/2026",
new CultureInfo("en-GB"), DateTimeStyles.None, out DateTime d2);
Call
What it does
DateTime.TryParse(text, out DateTime d)
Use the current culture.
DateTime.TryParse(text, culture, styles, out d)
Choose a culture and styles.
if (DateTime.TryParse(...)) { ... }
Check the bool before you use the date.
If the result is false, the out variable equals default(DateTime), which is DateTime.MinValue (January 1, year 1). Never use it without checking the bool.
Library declarations
C#
public static bool TryParse(string? s, out DateTime result);
public static bool TryParse(string? s, IFormatProvider? provider, DateTimeStyles styles, out DateTime result);
Parameters
Parameter
Description
s
The text to read. Null or empty gives false.
provider
Optional. A culture such as CultureInfo.InvariantCulture.
styles
Optional. DateTimeStyles flags such as None or AssumeUniversal.
result
out parameter. The parsed date, or DateTime.MinValue on failure.
Return Value
Return Value
Description
bool
true if the text was converted; otherwise false.
C# Examples
Example 1 — True or False
Parse good text and bad text and print the result and the default value.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
bool ok = DateTime.TryParse("2026-11-18", out DateTime good);
Console.WriteLine(ok + " " + good.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
bool bad = DateTime.TryParse("hello", out DateTime none);
Console.WriteLine(bad);
Console.WriteLine(none == DateTime.MinValue);
}
}
Output
True 2026-11-18
False
True
How It Works
Good text.2026-11-18 is valid, so the method returns True and fills good.
Bad text.hello is not a date, so the method returns False without throwing.
Default result. On failure the out value is DateTime.MinValue.
Check first. The last line proves the failed result is only a placeholder.
Example 2 — en-GB vs Invariant
Try the same day-first text with two cultures.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
string text = "18/11/2026";
bool uk = DateTime.TryParse(text, new CultureInfo("en-GB"),
DateTimeStyles.None, out DateTime d1);
Console.WriteLine(uk + " " + d1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
bool inv = DateTime.TryParse(text, CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime d2);
Console.WriteLine(inv);
}
}
Output
True 2026-11-18
False
How It Works
One text, two cultures. The same 18/11/2026 goes through both parsers.
en-GB. It expects day first, so 18 is the day and the call succeeds.
Invariant. It expects month first, and month 18 does not exist, so the call fails.
No crash. The second result is just False; no exception is thrown.
Example 3 — Check a List of Inputs
Loop over several user-style inputs and print a result for each.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
string[] inputs = { "2026-11-18", "2026-02-30", "11/18/2026" };
foreach (string text in inputs) {
if (DateTime.TryParse(text, CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime d)) {
Console.WriteLine(text + " -> " + d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
} else {
Console.WriteLine(text + " -> invalid");
}
}
}
}