-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Description
DateTime.ParseExact
fails to parse German date strings like "10. Okt. 2025"
using format "dd. MMM. yyyy"
and culture "de-DE"
in .NET 7+, even though the format explicitly includes the dot after the month abbreviation as a literal.
Steps to Reproduce
var date = "10. Okt. 2025";
var formats = new[] { "dd. MMM. yyyy" };
var culture = CultureInfo.CreateSpecificCulture("de-DE");
DateTime.ParseExact(date, formats, culture, DateTimeStyles.None);
Expected Behavior
Should parse successfully. The format string includes a literal dot after MMM
, so "Okt."
should be interpreted as "Okt"
(month) + "."
(literal) as it is parsed with any other special character.
Actual Behavior
Throws FormatException
: string '10. Okt. 2025' was not recognized as a valid DateTime.
Regression
Works with .Net 4.81
Root Cause Hypothesis
Other than .Net 4.81, .NET 7+ uses ICU globalization, which returns "Okt"
(without dot) in AbbreviatedMonthNames
. ParseExact
matches "MMM"
against this list and fails to parse "Okt."
, treating the dot as part of the month name rather than a literal.
Evidence of Inconsistency
Other literal characters (e.g., +
, -
, *
) placed after MMM
in the format string work correctly with PowerShell 7.5.3 (.Net 7+) and 5.1: (.Net 4.8.1)
$Date = '10. Okt+ 2025'
$DateFormat = @('dd. MMM+ yyyy')
$Culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('de-DE')
[datetime]::ParseExact($Date, $DateFormat, $Culture, [System.Globalization.DateTimeStyles]::None)
# Output: Freitag, 10. Oktober 2025 00:00:00
This proves that ParseExact
respects literal characters — except for the dot (.
), which is mishandled.
Known Workarounds
No workaround. User must change his Input by removing the dot ot the developer must changed the code by using 'TryParseExact'
Configuration
Powershell 5.1.26100.6725 on Net 4.8.1 and Powershell 7.5.3 with Net 7+ (9.0 is installed)
Windows 11 26200.6725
Architecture: x64
Behavior is not configuration but .Net specific.
Other information
Why This Is a Bug
- The dot is part of the format string, not the month name.
ParseExact
should tokenize"MMM."
as a format token followed by a literal dot.- This breaks compatibility with .NET Framework and violates the documented behavior of
ParseExact
.
Suggested Fix
Ensure ParseExact
respects literal characters in the format string and does not rely solely on AbbreviatedMonthNames
for MMM
. The parser should treat "MMM."
as a format token followed by a literal dot.