C# DateTime ParseExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

In C#, DateTime.ParseExact reads text only when it follows the format you give. A format such as dd-MM-yyyy means two-digit day, dash, two-digit month, dash, four-digit year.

It is stricter than Parse. That is good for files, APIs, and forms where only one shape is allowed. If the text does not match, it throws FormatException.

This is a static member of System.DateTime. You call it with the type name, such as DateTime.ParseExact(...). DateTime holds a date and time of day with a Kind of Utc, Local, or Unspecified.

Syntax

Pass the text, the exact format, and a culture.

C#
DateTime a = DateTime.ParseExact("18-11-2026", "dd-MM-yyyy", CultureInfo.InvariantCulture);
DateTime b = DateTime.ParseExact("2026-11-18",
    new[] { "dd-MM-yyyy", "yyyy-MM-dd" },
    CultureInfo.InvariantCulture, DateTimeStyles.None);
CallWhat it does
DateTime.ParseExact(s, "dd-MM-yyyy", culture)Text must match one format exactly.
DateTime.ParseExact(s, formats, culture, styles)Text may match any format in an array.
DateTime.ParseExact(s, "yyyyMMdd", culture)Works for compact text with no separators.

Use CultureInfo.InvariantCulture for fixed shapes such as file names and API values. Letters in the format are case-sensitive: MM is month and mm is minutes.

Library declarations
C#
public static DateTime ParseExact(string s, string format, IFormatProvider? provider);
public static DateTime ParseExact(string s, string format, IFormatProvider? provider, DateTimeStyles style);
public static DateTime ParseExact(string s, string[] formats, IFormatProvider? provider, DateTimeStyles style);

Parameters

ParameterDescription
sRequired. The text to read.
format / formatsRequired. One format string, or an array of allowed formats.
providerRequired. A culture such as CultureInfo.InvariantCulture, or null for the current culture.
styleOptional. DateTimeStyles flags, for example AllowWhiteSpaces.

Return Value

Return ValueDescription
DateTimeThe date and time read from the text. Missing parts (such as time) are set to zero.

C# Examples

Example 1 — Parse dd-MM-yyyy

Read day-month-year text with a custom format.

C#
using System;
using System.Globalization;

class Program {
    static string Show(DateTime d) => d.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime d = DateTime.ParseExact("18-11-2026", "dd-MM-yyyy", CultureInfo.InvariantCulture);
        Console.WriteLine(Show(d));
    }
}

How It Works

  1. Format letters. dd is the day, MM is the month, and yyyy is the year.
  2. Dashes match. The text has dashes in the same places as the format.
  3. Result. The first number (18) is read as the day, not the month.
  4. Midnight. The format has no time part, so the time is 00:00:00.

Example 2 — Date, Time, and AM/PM

Read a 12-hour clock value with the tt designator.

C#
using System;
using System.Globalization;

class Program {
    static string Show(DateTime d) => d.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

    static void Main() {
        DateTime d = DateTime.ParseExact(
            "2026/11/18 02:30 PM", "yyyy/MM/dd hh:mm tt", CultureInfo.InvariantCulture);
        Console.WriteLine(Show(d));
        Console.WriteLine(d.Hour);
    }
}

How It Works

  1. hh and tt. hh is a 12-hour clock and tt reads AM or PM.
  2. 02:30 PM. Afternoon hours are shifted by twelve when stored.
  3. 24-hour output. The helper prints 14:30:00, so the PM was understood.
  4. Hour property. d.Hour is 14, which confirms the conversion.

Example 3 — Wrong Shape and Many Formats

Show a mismatch exception, then allow two formats with an array.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        CultureInfo inv = CultureInfo.InvariantCulture;
        try {
            DateTime.ParseExact("2026-11-18", "dd-MM-yyyy", inv);
        } catch (FormatException) {
            Console.WriteLine("FormatException");
        }

        string[] formats = { "dd-MM-yyyy", "yyyy-MM-dd" };
        DateTime ok = DateTime.ParseExact("2026-11-18", formats, inv, DateTimeStyles.None);
        Console.WriteLine(ok.ToString("yyyy-MM-dd", inv));
    }
}

How It Works

  1. Wrong order. The text starts with the year, but the format expects the day first.
  2. FormatException. ParseExact refuses text that does not follow the format.
  3. Array of formats. The second call lists two allowed shapes.
  4. Second shape matches. yyyy-MM-dd fits, so the date is read and printed.

Frequently Asked Questions

Parse guesses the format. ParseExact accepts only the format you give and throws if the text does not match.
MM is the month number. mm is the minutes. Using the wrong case gives wrong or failing results.
Yes. Pass a string array of formats with a culture and DateTimeStyles.
Use CultureInfo.InvariantCulture for fixed shapes. Use a real culture only for text typed by people in that culture.
Use TryParseExact. It returns false instead of throwing when the text does not match.

Next: SpecifyKind()

Create a DateTime with the same ticks but a different Kind (Utc, Local, or Unspecified) with System.DateTime.

SpecifyKind() 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.

11 people found this page helpful