C#
DateTime
TryParseExact
debugging
.NET

DateTime.TryParseExact rejecting valid formats

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

DateTime.TryParseExact is strict by design. When it rejects a string that looks valid to a human, the reason is usually a mismatch in format tokens, culture, whitespace, or style flags rather than a bug in the parser.

Exact Means Exact

TryParseExact does not "guess" the format. The input string must match the supplied pattern precisely.

csharp
1using System;
2using System.Globalization;
3
4var input = "2026-03-07 14:30";
5var ok = DateTime.TryParseExact(
6    input,
7    "yyyy-MM-dd HH:mm",
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.None,
10    out var value
11);
12
13Console.WriteLine(ok);
14Console.WriteLine(value);

If you change the format string even slightly, parsing can fail.

The Most Common Token Mistakes

Several token pairs are easy to confuse:

  • 'MM means month, while mm means minute'
  • 'HH means 24-hour clock, while hh means 12-hour clock'
  • 'dd means day of month, while ddd or dddd means day name'
  • 'yyyy means four-digit year, while yy means two-digit year'

This example fails because the format is wrong for the input:

csharp
1using System;
2using System.Globalization;
3
4var input = "2026-03-07 14:30";
5var ok = DateTime.TryParseExact(
6    input,
7    "yyyy-mm-dd HH:MM",
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.None,
10    out _
11);
12
13Console.WriteLine(ok); // false

It looks close, but the month and minute tokens were swapped.

Culture Matters More Than Many People Expect

Month names, day names, separators, and even ordering rules can be culture-dependent.

csharp
1using System;
2using System.Globalization;
3
4var input = "07-Mar-2026";
5var ok = DateTime.TryParseExact(
6    input,
7    "dd-MMM-yyyy",
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.None,
10    out var value
11);
12
13Console.WriteLine(ok);
14Console.WriteLine(value.ToString("O"));

If you parse month names with the wrong culture, the same text can fail on one machine and succeed on another. For stable interchange formats, CultureInfo.InvariantCulture is usually the right default.

Whitespace and Styles Can Break Otherwise Good Input

TryParseExact can reject input because of leading or trailing spaces unless your style flags allow them.

csharp
1using System;
2using System.Globalization;
3
4var input = " 2026-03-07 ";
5
6var ok = DateTime.TryParseExact(
7    input,
8    "yyyy-MM-dd",
9    CultureInfo.InvariantCulture,
10    DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite,
11    out var value
12);
13
14Console.WriteLine(ok);
15Console.WriteLine(value);

If the source data is external, style flags often matter as much as the format string.

AM and PM Must Match the Clock Format

If your input uses AM or PM, the format must include it and must use a 12-hour token.

csharp
1using System;
2using System.Globalization;
3
4var input = "03/07/2026 02:15 PM";
5var ok = DateTime.TryParseExact(
6    input,
7    "MM/dd/yyyy hh:mm tt",
8    CultureInfo.InvariantCulture,
9    DateTimeStyles.None,
10    out var value
11);
12
13Console.WriteLine(ok);
14Console.WriteLine(value);

Using HH with tt is inconsistent. Some combinations may still behave unexpectedly, so keep the tokens aligned with the actual clock representation.

Use Multiple Formats When the Input Is Not Uniform

If the incoming data can legitimately appear in several exact formats, pass an array instead of trying to force one pattern to do too much.

csharp
1using System;
2using System.Globalization;
3
4var input = "2026/03/07";
5var formats = new[] { "yyyy-MM-dd", "yyyy/MM/dd", "dd-MM-yyyy" };
6
7var ok = DateTime.TryParseExact(
8    input,
9    formats,
10    CultureInfo.InvariantCulture,
11    DateTimeStyles.None,
12    out var value
13);
14
15Console.WriteLine(ok);
16Console.WriteLine(value);

That keeps parsing rules explicit without falling back to looser parsing behavior.

Debugging Strategy

When TryParseExact fails unexpectedly, inspect these in order:

  1. The raw input, including whitespace.
  2. The exact format string.
  3. The culture provider.
  4. The style flags.
  5. Whether the input truly has one format or multiple.

In practice, token mistakes and culture mismatches explain most failures.

Common Pitfalls

The most common mistake is mixing up MM and mm, or HH and hh. Those bugs look visually minor but completely change what the parser expects.

Another issue is using the current machine culture by accident. That can make tests pass locally and fail in production.

A third problem is assuming TryParseExact tolerates extra spaces or alternate separators. It does not unless your format and styles explicitly allow them.

Summary

  • 'TryParseExact requires a precise match between input and format string.'
  • Format token mistakes such as MM versus mm are a common cause of failure.
  • Use CultureInfo.InvariantCulture for stable machine-readable formats.
  • Add style flags when leading or trailing whitespace is expected.
  • Pass multiple exact formats when the input is legitimately non-uniform.

Course illustration
Course illustration

All Rights Reserved.