6

How to change DateTimeFormatInfo.CurrentInfo AbbreviatedDayNames collection. I want to use mine, like Tues instead of Tue. And when I use ToString("ddd") I want to see Tue.

Is it possible in C#?

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
Sergey
  • 7,933
  • 16
  • 49
  • 77

3 Answers3

7

You may do it in this fashion

CultureInfo cinfo = CultureInfo.CreateSpecificCulture("en-EN");
cinfo.DateTimeFormat.AbbreviatedDayNames = new string[]{"Suns","Mon","Tues", "Weds","Thursday","Fries","Sats"};

Now assign this as your current culture and you should be good to go

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
2

Create new user-writeable CultureInfo and then fill your values

    var dt = DateTime.Now;
    CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, true); // second parameter is useUserOverride
    Console.WriteLine(dt.ToString("ddd", ci));
    ci.DateTimeFormat.AbbreviatedDayNames = new string[] { "D1", "D2", "D3", "D4", "D5", "D6", "D7" };
    Console.WriteLine(dt.ToString("ddd", ci));
Ondra
  • 1,619
  • 13
  • 27
  • userUserOverride isn't what allows you to edit the culture here. useUserOverride tells .net whether to use the standard culture or whether to accept any customizations the user has made through the regional and language settings. It's normally a good idea to use the user overrides though when presenting to the user. You wouldn't want them for the invariant culture however. – Chris Chilvers Jul 14 '11 at 12:07
  • My bad, Thanks for noting this – Ondra Jul 14 '11 at 12:17
1

According to this msdn link the AbbreviatedDayNames collection is read/write, which would imply that you could overwrite it. However as it is culture dependent, this would only work when a culture is set and used ( the invariantinfo is readonly ).

You might be able to/have to create a new culture to use for this, but I reckon that is OTT.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33