Dave Stockinger
Dave Stockinger

Reputation: 139

DateTime.ParseExact doesn't work as i expect

I'm trying to parse a DateTime (in C#) from en-US to de-DE or just in a different Format. The input DateTime is 06/17/2015 09:22:30 AM.

What i've tried:

DateTime date = DateTime.ParseExact(dateString, "d.MM.yyyy HH:mm:ss", new CultureInfo("de-DE"));

or

DateTime date = DateTime.ParseExact(dateString, "d.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);

in both versions i have the same output - that means the output is the same as the input, nothing has changed. I can change it in the ToString() method but i need it as a DateTime.

string test = datum.ToString("d.MM.yyyy HH:mm:ss");
output is `17.06.2015 09:22:30`

Could the CurrentCultureInfo of the current Thread be the problem? Because the current Thread is set to CultureInfo en-US. I've set the CultureInfo.DefaultThreadCurrentUICulture to de-DE but it didn't work either.

What am i missing? I'm sure it's just a missing point in my head. If the solution is really easy - sorry for waisting time - anyway, thank you for every answer!

Upvotes: 1

Views: 843

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460158

That's the default format of CultureInfo.InvariantCulture, so you don't need to use ParseExact:

DateTime date = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
string inGermanFormat = date.ToString("d.MM.yyyy HH:mm:ss", new CultureInfo("de-DE"));

Upvotes: 3

Related Questions