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);
Call
What 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
Parameter
Description
s
Required. The text to read.
format / formats
Required. One format string, or an array of allowed formats.
provider
Required. A culture such as CultureInfo.InvariantCulture, or null for the current culture.
style
Optional. DateTimeStyles flags, for example AllowWhiteSpaces.
Return Value
Return Value
Description
DateTime
The 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));
}
}
Output
2026-11-18 00:00:00
How It Works
Format letters.dd is the day, MM is the month, and yyyy is the year.
Dashes match. The text has dashes in the same places as the format.
Result. The first number (18) is read as the day, not the month.
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);
}
}
Output
2026-11-18 14:30:00
14
How It Works
hh and tt.hh is a 12-hour clock and tt reads AM or PM.
02:30 PM. Afternoon hours are shifted by twelve when stored.
24-hour output. The helper prints 14:30:00, so the PM was understood.
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));
}
}
Output
FormatException
2026-11-18
How It Works
Wrong order. The text starts with the year, but the format expects the day first.
FormatException. ParseExact refuses text that does not follow the format.
Array of formats. The second call lists two allowed shapes.
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.