✅ DateOnly.TryParse Vs DateOnly.TryParseExact use cases
Hello guys, I was reading a bit about
DateOnly
data type. I didn't understand the difference between DateOnly.TryParse
and DateOnly.TryParseExact
. Normally, when we use the parse method, the string given is parsed according to our system culture? How does TryParse and TryParseExact differs?5 Replies
Now, the thing is, if we successfully parse the dates, for the
TryParse
for example, the date would be stored in the format based on our system culture? While for the TryParseExact
it would be as the format we specified ?The culture impacts on how you try to parse the string, not how it is stored (in memory) theoretically both in memory representations of your Date are equal
The difference is how "lenient" both methods are for differences in the string Vs the supplied format string
TryParse
doesn't have an overload that lets you specify a format string like TryParseExact
does. It only lets you specify an IFormatProvider
and DateTimeStyles
. But yes it is about how strictly your input string must match the provided formatTrue, I've been programming in some weird languages as of recently and they have format strings in the "basic" parse method
But yeah, TryParse will try to find an appropriate format to parse the date, TryParseExact will try to parse it according to specific formats you supply
as for the use cases. When you have control over the format of the string, then use
TryParseExact
to enforce a consistent style. When the format may vary, use TryParse