Failed to parse DateTime

I am trying to parse a date / time string using DateTime.ParseExact . It works everywhere, with the exception of one machine - it simply will not understand this machine. The question is why? What could be different on this machine to cause this behavior?

Here are some things I've already looked at:

  • CultureInfo is passed to DateTime.ParseExact , namely CultureInfo.InvariantCulture
  • The regional settings on the rogue machine are the same as the settings on the machine that is parsing.
  • Yes, the string is correct in the format dd/MM/yyyy HH:mm:ss
+4
source share
4 answers

I always find that regional settings can be complicated, and you can never assume that users of your application will even properly install their computers!

The trap that I used to parse dates, if they should be strings, is to parse it in the format "dd / MMM / yyyy", for example. "14 / JAN / 2009" will be perfectly broadcast regardless of the settings.

By the way, this trick also works with SQL Server :)

+6
source

It is very difficult to guess what solution might be without information about the exception (as Mark Gravell asked about) and / or some code example.

In my experience, I had date / time problems due to cultural issues. You said you already did hard coding.

what about the actual culture in which the process is carried out? if this code is on the asp.net website, the culture is set based on the user's browser settings (passed in the request).

try doing this in your code to hardcode the current thread culture to see if this help helps as a way to debug this problem.

 // Replace the culture with whatever you required. System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); 

Tell me what happens when you try to do this. ** I hate entering answers when we don't have enough information. this is more a suggestion than an answer :)

+1
source

Verify that the area settings on the computer that are not working match the machines that are running. To avoid similar problems in the future, indicate the culture when parsing.

0
source

I agree with Pure.Krome that you need more information. To this end, questions:

  • What exception is thrown?
  • What method of overload do you call, and what exactly do you pass to it?
  • Have you confirmed that the version of .NET execution is the same for all mailboxes? Including service pack version?
  • Is the line you are processing in your program, or does it come from an external source? If it is external, have you confirmed that the string is incorrectly escaped in the rogue window? Perhaps the version of any program that creates the string is outdated and has an error in it.
0
source

All Articles