Search code examples
.netdatetimeasp.net-corecurrentculture

Datetime.ParseExact inverse day and month value


var dateString = "23/12/2019 06:30:00";
DateTime dt = DateTime.ParseExact(recordDateString.Trim(), "dd/MM/yyyy HH:mm:ss", new CultureInfo("fr-FR"));

I expect this output : 23/12/2019 06:30:00 But dt object value is : 12/23/2019 06:30 PM

Where is wrong here?

Note: The code is written in .net core 2.2


Solution

  • Your dt variable contains what you expect - December 23rd, 2019. But when you print it, it is probably printed with the en-US locale, which uses MM/dd/yyyy. The DateTime type only stores the actual time value, not the locale (so it kinda forgets that you parsed it from fr-FR), it can be parsed from any locale and printed to any other locale. Try dt.ToString(new CultureInfo("fr-FR")).