Search code examples
c#stringdatetimestring-formattingculture

When parsing datetime into month day with English culture, it is still parsed in Turkish language


Here is the code. I still get incorrect results

string srRegisterDate = "25.07.2009 00:00:00"
CultureInfo culture = new CultureInfo("en-US");
srRegisterDate = String.Format("{0:dddd, MMMM d, yyyy}", Convert.ToDateTime(srRegisterDate), culture);

The result is Cumartesi, Temmuz 25, 2009

Instead it should be Saturday, July 25, 2009

How can i fix this error?

C#.net v4.5.2


Solution

  • The issue is because of ignored culture value in String.Format and not with converting string to DateTime.

    You need:

    srRegisterDate = String.Format(culture, "{0:dddd, MMMM d, yyyy}", Convert.ToDateTime(srRegisterDate));
    

    Your current call to String.Format utalizes the overload String.Format Method (String, Object[]) and the culture passed in parameter is treated as a simple object parameter passed for place holder. Since there is no place holder, you don't even see it getting ignored.

    If you want to have culture utilized in String.Format then you have to use the overload : Format(IFormatProvider, String, Object) or String.Format Method (IFormatProvider, String, Object[])