Home > OS >  TryParseExact from french dates with month full name
TryParseExact from french dates with month full name

Time:10-18

I need to parse dates such as

7 août 2008 (August 7th 2008)

I tried this :

var french = CultureInfo.GetCultureInfo("fr-FR");
DateTime.TryParseExact(input, "d MMMM yyyy", french, DateTimeStyles.None, out parsed_date);

which returns false

However, this code

var french = CultureInfo.GetCultureInfo("fr-FR");
var test = new DateTime(2008, 8, 7);
Console.WriteLine(test.ToString("d MMMM yyyy", french));

outputs

7 août 2008

as expected.

What am I doing wrong here ?

CodePudding user response:

TryParseExact is actually Exact.

In my case, I needed to ignore white spaces (probably not printable) using

var styles = DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AllowTrailingWhite;
var french = CultureInfo.GetCultureInfo("fr-FR");
DateTime.TryParseExact(input, "d MMMM yyyy", french, DateTimeStyles.None, out parsed_date);

to get the expected result.

CodePudding user response:

Your code works for me.

string input = "7 août 2008";
DateTime parsed_date;
var french = CultureInfo.GetCultureInfo("fr-FR");
DateTime.TryParseExact(input, "d MMMM yyyy", french, DateTimeStyles.None, out parsed_date);

This code returns true and gives me the currect date in parsed_date.

  • Related