Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 812
MonsterMMORPG Avatar asked May 15 '15 13:05

MonsterMMORPG


People also ask

How does DateTime Parse work?

The Parse method tries to convert the string representation of a date and time value to its DateTime equivalent. It tries to parse the input string completely without throwing a FormatException exception.

Which is an example of correctly parsing a date time string into a DateTime object in net?

DateTime dateTime10 = DateTime. ParseExact(dateString, "mm/dd/yyyy", provider); dateString = "not a date"; // Exception: The string was not recognized as a valid DateTime.

How to parse date and time strings with invariant culture?

To prevent the difficulties in parsing data and time strings that are associated with changes in cultural data, you can parse date and time strings by using the invariant culture, or you can call the ParseExact or TryParseExact method and specify the exact format of the string to be parsed.

How to parse a specific date and time format across locales?

To parse a specific date and time format across different locales, use one of the overloads of the DateTime.ParseExact method and provide a format specifier. All overloads of the Parse method are culture-sensitive unless the string to be parsed (which is represented by s in the following table) conforms to the ISO 8601 pattern.

What is the difference between parse and parseexact in DATEA?

A non-standard date and time string can be complicated and difficult to parse. The Parse method tries to parse a string with several implicit parse patterns, all of which might fail. In contrast, the ParseExact method requires you to explicitly designate one or more exact parse patterns that are likely to succeed.

How does the parse method work with time zones?

Generally, the Parse method returns a DateTime object whose Kind property is DateTimeKind.Unspecified. However, the Parse method may also perform time zone conversion and set the value of the Kind property differently, depending on the values of the s and styles parameters:


1 Answers

Convert.ToDateTime(srRegisterDate) still uses your CurrentCulture which looks like tr-TR based on day and month names. Cumartesi - Temmuz

In String.Format, your culture will be ignored since you used it as a third parameter as an argument but you never use it.

Your code called Format(string format, object arg0, object arg1) overload. But you never use the arg1. This overload doesn't think this is a culture. It thinks it is another argument that you want to format.

This String.Format has an overloads that takes IFormatProvider as a first argument.

  • Format(IFormatProvider, String, Object)
  • Format(IFormatProvider, String, Object[])
  • Format(IFormatProvider, String, Object, Object)
  • Format(IFormatProvider, String, Object, Object, Object)

For example;

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

But most comman way (and I always prefer) to get the result that you want is using .ToString() method like;

srRegisterDate = Convert.ToDateTime(srRegisterDate).ToString("dddd, MMMM d, yyyy", culture);
like image 90
Soner Gönül Avatar answered Oct 24 '22 11:10

Soner Gönül