I'm working with TryParseExact but it's throwing an error.
What could be the issue? Thanks
DateTime result;
if (DateTime.TryParseExact("31 December, 2023", ["'dd MMMM, yyyy'"],
CultureInfo.InvariantCulture,
DateTimeStyles.None, out result))
{
// Success
}
else
{
// Failure
}
I've tried escaping the date format as "dd MMMM',' yyyy" and using DateTimeStyles.AdjustToUniversal but there's no effect.
When you see the `Exact` in `TryParseExact()`, **it means it**; the method is _super strict_ about the input _exactly matching_ the specified format.~~In this case, the single quotes in this string are part of the expected format:
To match this format, the date would need to look like this:
You probably want to use this format string instead:Finally, I know it's probably just an example, but if you really have a constant value you're also generally better off using the
DateTimeconstructor, like this:or this, if you're not yet comfortable with the newer syntax: