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
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.
DateTime dateTime10 = DateTime. ParseExact(dateString, "mm/dd/yyyy", provider); dateString = "not a date"; // Exception: The string was not recognized as a valid DateTime.
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.
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.
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.
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:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With