C# DateTime TryParse() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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);
CallWhat 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

ParameterDescription
sThe text to read. Null or empty gives false.
providerOptional. A culture such as CultureInfo.InvariantCulture.
stylesOptional. DateTimeStyles flags such as None or AssumeUniversal.
resultout parameter. The parsed date, or DateTime.MinValue on failure.

Return Value

Return ValueDescription
booltrue 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);
    }
}

How It Works

  1. Good text. 2026-11-18 is valid, so the method returns True and fills good.
  2. Bad text. hello is not a date, so the method returns False without throwing.
  3. Default result. On failure the out value is DateTime.MinValue.
  4. 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);
    }
}

How It Works

  1. One text, two cultures. The same 18/11/2026 goes through both parsers.
  2. en-GB. It expects day first, so 18 is the day and the call succeeds.
  3. Invariant. It expects month first, and month 18 does not exist, so the call fails.
  4. 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");
            }
        }
    }
}

How It Works

  1. Three inputs. One is ISO, one is impossible, and one is month-first text.
  2. if with TryParse. The bool result decides which branch runs.
  3. February 30. That day does not exist, so the check fails and prints invalid.
  4. Invariant order. 11/18/2026 is month first, which the invariant culture accepts.

Frequently Asked Questions

It returns true if the text is a valid date and false otherwise. The date is in the out parameter.
DateTime.MinValue (0001-01-01 00:00:00).
No for bad text. It only returns false.
Date order differs by culture. 18/11/2026 is day first in en-GB, but the invariant culture expects month first.
Use it when only one exact shape, such as dd-MM-yyyy, is allowed.

Next: TryParseExact()

Try to convert text that matches an exact format; returns true or false instead of throwing with System.DateTime.

TryParseExact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful