0

I reviewed the question here: Convert string to datetime in C#.net

The format I'm trying to pass is only slightly different, but cannot get it to work.

My code:

var date = DateTime.ParseExact(@"28/06/2012 06:04:10 PM", @"dd/MM/yyyy hh:mm:ss tt", null);

So then I tried the example code from the question mentioned above:

var date = DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", null);

That doesn't work either. Both give me

System.FormatException
String was not recognized as a valid DateTime.

Any ideas would be greatly appreciated! Thanks!

1
  • I assume this is some kind of localisation issue. What culture are you? Have you tried it with invariant culture stuff? Commented Mar 10, 2014 at 16:46

3 Answers 3

1

The problem is localisation.

Consider these three statements:

DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", new CultureInfo("fr-fr"))
DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", new CultureInfo("en"))
DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", CultureInfo.InvariantCulture)

The first will not work whereas the last two will. In this case it is because the PM is not valid in fr-fr. If you try this:

DateTime.ParseExact(@"14/04/2010 10:14:49.", @"dd/MM/yyyy hh:mm:ss.tt", new CultureInfo("fr-fr"))

it will work fine.

As has been noted in comments other cultures may fail on other items. en-za uses a different date separator causing that to fail.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Chris! Seems to work if I use CultureInfo.InvariantCulture. Using CultureInfo.CurrentCulture breaks, which I don't understand. CurrentCulture is en-us. The 'PMDesignator' for en-us is set as 'PM', so why would this still fail? Same goes for en-za.
So if you do CultureInfo.CurrentCulture.Name it tells you it is en-us? That doesn't seem right since the above statements if used with explicitly en-us works for me. In the case of en-za it looks like it is probably the date separator that is causing problems though. Changing the / for - (culture.DateTimeFormat.DateSeparator) makes it work. This is why specifying the culture is so necessary.
0
var dateString = "28/06/2012 06:04:10 PM";
DateTime dt = DateTime.ParseExact(dateString, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

Comments

0

/ is date separator and probably in your culture it's not exactly /. The same issue can occur with :, which is replaced with current culture time separator. Try escaping both / and ::

var date = DateTime.ParseExact(@"28/06/2012 06:04:10 PM", @"dd\/MM\/yyyy hh\:mm\:ss tt", null);

1 Comment

Indeed, @Chris is correct. The PM designator doesn't seem to work on the current culture (en-us), whereas if I simply use InvariantCulture it works....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.