9

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

Amal Dev
  • 1,938
  • 1
  • 14
  • 26
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342

3 Answers3

7

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.

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
5

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[])

Habib
  • 219,104
  • 29
  • 407
  • 436
-1

This works

DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo trDtfi = new CultureInfo("tr-TR", false).DateTimeFormat;
string result = String.Format("{0:dddd, MMMM d, yyyy}", Convert.ToDateTime(srRegisterDate, trDtfi).ToString(usDtfi));

original

25.07.2009 00:00:00

transformed

7/25/2009 12:00:00 AM

EDIT

Was one to short of required

string test = String.Format("{0:dddd, MMMM d, yyyy}", Convert.ToDateTime(Convert.ToDateTime(srRegisterDate, trDtfi).ToString(usDtfi)));

Saturday, July 25, 2009

CheGueVerra
  • 7,849
  • 4
  • 37
  • 49
  • This is not what OP wants. – Soner Gönül May 15 '15 at 14:11
  • yes that will work, but there is **another issue** with this. There is **no need of `String.Format`**, your format will be ignored since you are **passing a string parameter and not a `DateTime`** parameter. Consider the example `string result = String.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(srRegisterDate, trDtfi).ToString(usDtfi));` with your code, – Habib May 15 '15 at 14:12
  • Yeah I noticed was to happy to solve the culture transform .. sry OP – CheGueVerra May 15 '15 at 14:13
  • Again, with your EDIT `string test = String.Format("{0:dddd, MMMM d, yyyy}", Convert.ToDateTime(Convert.ToDateTime(srRegisterDate, trDtfi).ToString(usDtfi))); ` , you are converting, string to DateTime, and then Converting it to string and then again converting it to DateTime to pass to `String.Format`, do you see the issue with that ? – Habib May 15 '15 at 14:29
  • Obviously I don't so enlighten me, I don't mind losing rep, I want to know the best way. The end result gives what the OP expected though – CheGueVerra May 15 '15 at 14:30