Home C# DateTime Methods TryParseExact() C# DateTime TryParseExact() Method Definition In C#, DateTime.TryParseExact combines the strictness of ParseExact with the safety of TryParse. The text must match your format, and a bad match returns false instead of an exception.
It is the best choice for forms, file imports, and API values that must follow a known pattern.
This is a static member of System.DateTime. You call it with the type name, such as DateTime.TryParseExact(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.
Syntax Pass the text, one format (or an array), a culture, styles, and an out DateTime.
bool ok = DateTime.TryParseExact("18-11-2026", "dd-MM-yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime d);
string[] formats = { "yyyy-MM-dd", "dd/MM/yyyy" };
bool ok2 = DateTime.TryParseExact(text, formats,
CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime d2); Call What it does DateTime.TryParseExact(s, "dd-MM-yyyy", culture, styles, out d)One exact format. DateTime.TryParseExact(s, formats, culture, styles, out d)Any one of several formats. DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversalRead text as UTC and return a Utc value.
Always check the bool. On failure, result is DateTime.MinValue. Format letters are case-sensitive: MM month, mm minutes, HH 24-hour, hh 12-hour.
Library declarations public static bool TryParseExact(string? s, string? format, IFormatProvider? provider, DateTimeStyles style, out DateTime result);
public static bool TryParseExact(string? s, string?[]? formats, IFormatProvider? provider, DateTimeStyles style, out DateTime result); Parameters Parameter Description s The text to read. Null or empty gives false. format / formats One format string, or an array of allowed formats. provider A culture, often CultureInfo.InvariantCulture. style DateTimeStyles flags, such as None or AllowWhiteSpaces.result out parameter. The parsed date, or DateTime.MinValue on failure.
Return Value Return Value Description bool true if the text matched a format; otherwise false.
Important
Styles change Kind: without styles, the result has Kind.Unspecified. With AssumeUniversal | AdjustToUniversal you get a Utc value. AssumeUniversal alone converts the result to local time, which surprises many beginners.
C# Examples 1. Match or not 2. Many formats 3. Styles and Kind Example 1 — One Exact Format Accept dd-MM-yyyy and reject year-first text.
using System;
using System.Globalization;
class Program {
static void Main() {
CultureInfo inv = CultureInfo.InvariantCulture;
bool a = DateTime.TryParseExact("18-11-2026", "dd-MM-yyyy", inv, DateTimeStyles.None, out DateTime d1);
Console.WriteLine(a + " " + d1.ToString("yyyy-MM-dd", inv));
bool b = DateTime.TryParseExact("2026-11-18", "dd-MM-yyyy", inv, DateTimeStyles.None, out DateTime d2);
Console.WriteLine(b);
}
} How It Works Matching text. 18-11-2026 follows dd-MM-yyyy exactly, so the result is True.Printed date. The value is converted back to yyyy-MM-dd to prove the day and month were read correctly.Wrong order. The second text starts with the year, so the check returns False.No exception. A mismatch is a normal answer here, not an error.Example 2 — Accept Three Shapes Allow several formats and print the same date for each.
using System;
using System.Globalization;
class Program {
static void Main() {
CultureInfo inv = CultureInfo.InvariantCulture;
string[] formats = { "yyyy-MM-dd", "dd/MM/yyyy", "MMM dd yyyy" };
string[] inputs = { "2026-11-18", "18/11/2026", "Nov 18 2026" };
foreach (string text in inputs) {
if (DateTime.TryParseExact(text, formats, inv, DateTimeStyles.None, out DateTime d)) {
Console.WriteLine(d.ToString("yyyy-MM-dd", inv));
}
}
}
} How It Works Format list. The array holds three allowed shapes: ISO, day-first, and month-name.Three inputs. Each text fits a different entry in the list.One call, any match. TryParseExact tries each format until one fits.Same result. All three lines print the same normalized date.Example 3 — Read Text as UTC Compare the Kind of a default parse with a parse that uses AssumeUniversal and AdjustToUniversal.
using System;
using System.Globalization;
class Program {
static void Main() {
CultureInfo inv = CultureInfo.InvariantCulture;
string text = "2026-11-18 10:00";
string fmt = "yyyy-MM-dd HH:mm";
DateTime.TryParseExact(text, fmt, inv, DateTimeStyles.None, out DateTime plain);
DateTime.TryParseExact(text, fmt, inv,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out DateTime utc);
Console.WriteLine(plain.Kind);
Console.WriteLine(utc.Kind);
Console.WriteLine(utc.ToString("yyyy-MM-dd HH:mm:ss", inv));
}
} Unspecified
Utc
2026-11-18 10:00:00 How It Works Same text twice. The two calls differ only in the DateTimeStyles argument.Default style. The first result is Unspecified, because the text carries no zone.Universal styles. AssumeUniversal says the text is UTC and AdjustToUniversal keeps it as UTC.Hour is unchanged. The third line shows 10:00:00; only the Kind label differs.Frequently Asked Questions What is TryParseExact? It reads date text in an exact format and returns true or false instead of throwing.
What is the difference between TryParse and TryParseExact? TryParse accepts many shapes. TryParseExact accepts only the formats you list.
Can I list several formats? Yes. Pass a string array and the call succeeds if the text matches any of them.
What do AssumeUniversal and AdjustToUniversal do? AssumeUniversal treats text without a zone as UTC. Adding AdjustToUniversal returns a Utc value with the same clock time.
What is the out value if parsing fails? DateTime.MinValue. Always check the returned bool first.
Back to the method index See all 38 System.DateTime method guides grouped by what they do.
Method index → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion