I need to parse dates in my program, but it fails on the machine of one user (in denmark). It works for his colleagues who all has the same settings as far as I can tell.
The code looks as follows:
DateTime result;
if (DateTime.TryParse(InputBox.Text, out result))
YyyymmddField.Text = result.ToString("yyyyMMdd");
else
YyyymmddField.Text = "(invalid)";
CurrentCulture
is da-DK
and his configured format is: yyyy.MM.dd
. The date string I want to parse is 2015.07.14
.
This works on our machines here (in sweden) regardless of current culture. It also works for his colleagues, but not for him.
We have hundreds of users worldwide and as far as we know, his is the only computer that fails.
Could something other than the current CultureInfo
affect how TryParse
operates?
This works on our machines here (in sweden) regardless of current culture
No. There is no such a thing. If you use DateTime.TryParse
without any IFormatProvider
, it will use the CurrentCulture
settings of the current machine.
From documentation;
The string s is parsed using formatting information in the current
DateTimeFormatInfo
object, which is supplied implicitly by the current thread culture.
I'm using .NET Framework 4.5 and there are only 7 culture inside of AllCultures
that doesn't have this as standard date and time format. If you won't/can't tell us what is your other colleague's CurrentCulture
, I would assume he will use one of these from my perspective.
ar
bn
ml
ar-SA
bn-IN
ml-IN
bn-BD
Instead of that confusion, you can use InvariantCulture
is your string has a stable format like yyyy.MM.dd
instead of hoping his CurrentCulture
settings will parse it or not.
use CultureInfo.InvariantCulture
DateTime result;
if (DateTime.TryParse(InputBox.Text, out result))
YyyymmddField.Text = result.ToString("yyyyMMdd",CultureInfo.InvariantCulture);
else
YyyymmddField.Text = "(invalid)";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With