Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Date, Time and Globalization

DateTime.Parse() and TryParse()

Reference for parsing dates in C#: DateTime.Parse, TryParse, ParseExact and TryParseExact, DateTimeStyles, invariant culture and how to handle invalid input.

Parsing turns text into a DateTime. .NET offers two families. Parse and TryParse accept many common layouts and guess the format from the culture. ParseExact and TryParseExact accept only the format (or list of formats) you specify. All four exist on DateTime, DateTimeOffset, DateOnly and TimeOnly.

Use the Try versions for any text that comes from a user, a file or a network: they return false instead of throwing. Use ParseExact for machine formats where you know the layout, so that 03/04/2026 can never be read as March or April by accident.

C#
if (DateTime.TryParseExact("2026-03-15", "yyyy-MM-dd", CultureInfo.InvariantCulture,
        DateTimeStyles.None, out DateTime date))
{
    Console.WriteLine(date.DayOfWeek);
}

Try it

C# Parse, TryParse and ParseExact

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Styles, kinds and offsets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static DateTime Parse(string s)
public static DateTime Parse(string s, IFormatProvider? provider)
public static DateTime Parse(string s, IFormatProvider? provider, DateTimeStyles styles)
public static bool TryParse(string? s, IFormatProvider? provider, DateTimeStyles styles, out DateTime result)
public static DateTime ParseExact(string s, string format, IFormatProvider? provider)
public static DateTime ParseExact(string s, string[] formats, IFormatProvider? provider, DateTimeStyles style)
public static bool TryParseExact(string? s, string? format, IFormatProvider? provider, DateTimeStyles style, out DateTime result)
Parameter Description
s The text to parse. Overloads with ReadOnlySpan<char> avoid allocating a substring.
format / formats One or several exact patterns (standard or custom; see formatting).
provider Supplies names, order and separators. Use CultureInfo.InvariantCulture for stored text.
styles DateTimeStyles flags: AllowWhiteSpaces, AdjustToUniversal, AssumeUniversal, AssumeLocal, RoundtripKind, NoCurrentDateDefault.

Returns: the parsed value; TryParse and TryParseExact return a bool and set result to default on failure.

Errors: Parse and ParseExact throw FormatException when the text does not match, and ArgumentNullException for null. ParseExact also throws FormatException for invalid formats. A day like February 30 is a parse failure, not a rollover.

Usage notes

  • Never pass user text to Parse without a Try. Bad input is normal; exceptions are slow and noisy.
  • Always pass a culture. Without one, Parse uses the current culture, so 3/4/2026 is March 4 in one environment and April 3 in another. This sandbox is invariant (month first).
  • For known layouts use ParseExact. ISO 8601 with "O" or "yyyy-MM-dd'T'HH:mm:ss" is the best choice for storage and APIs.
  • Without styles, a zone in the text converts to local time. DateTime.Parse("...+02:00") returns local time with Kind.Local. Use RoundtripKind or parse to DateTimeOffset to keep the offset.
  • Text without a zone gets Kind.Unspecified unless you pass AssumeUniversal or AssumeLocal.
  • Exact formats are strict about digits. "yyyy-MM-dd" rejects 2026-3-5; use M and d for one-or-two-digit values.
  • Partial dates fill in defaults. A missing date means today, a missing time means midnight. Pass NoCurrentDateDefault to use 0001-01-01 instead of today for time-only text.
  • Related types parse the same way: DateOnly.TryParse, TimeOnly.TryParseExact, DateTimeOffset.TryParse, and TimeSpan.TryParse for durations.
  • Generic parsing. All of them implement IParsable<T> (.NET 7), so T.Parse(text, provider) works in generic code.