0

I'm trying to format the date and time on my app based on the users set culture info, however, every help resource I see keeps suggesting that I have to manually enter each culture locale in code. For example, if I wanted en-UK I would have to manually add new CultureInfo("en-UK"); with something like new CultureInfo("en-UK");.

Is there a way to just tap into the currently set culture on the phone without me having to actually type the rtc culture info in? Something that might work like "date = ConvertToLocalCultureFormat(date);"?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Euthyphro
  • 700
  • 1
  • 9
  • 26

3 Answers3

1

I don't know if this works on WinPhone7, but you could use

 CultureInfo.CurrentCulture.Name

which return the name of the CurrentCulture of the current thread (en-UK or whatever your app runs in)

See for refs

However this should not be necessary. If you convert your datetime to a string in this way:

  DateTime dt = DateTime.Now;
  // Converts dt, formatted using the ShortDatePattern
  // and the CurrentThread.CurrentCulture.
  string dateInString = dt.ToString("d");

you should get the conversion in the right CultureInfo of your phone.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks, I thought that it might need to be set otherwise since I've read a few articles complaining that it defaults to en-US. – Euthyphro Jun 03 '12 at 20:59
1

To format anything using the current culture, you don't have to do anything special at all. The overloads of all formatting that doesn't include a specific format or culture uses the default culture.

The Date.ToString() method for example will call the overload with this.ToString(CultureInfo.CurrentCulture) to pick up the current culture setting of the application and use for the formatting.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Which help resources did you read that suggest you have to manually specify the current culture?

The DateTime.ToString() parameterless method automatically uses formatting information derived from the current culture.

This method uses formatting information derived from the current culture. In particular, it combines the custom format strings returned by the ShortDatePattern and LongTimePattern properties of the DateTimeFormatInfo object returned by the Thread.CurrentThread.CurrentCulture.DateTimeFormat property.

DateTime exampleDate = new DateTime(2008, 5, 1, 18, 32, 6);
string s = exampleDate.ToString();
// Gives "5/1/2008 6:32:06 PM" when the current culture is en-US.
// Gives "01/05/2008 18:32:06" when the current culture is fr-FR.
// Gives "2008/05/01 18:32:06" when the current culture is ja-JP.
Douglas
  • 53,759
  • 13
  • 140
  • 188