Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change DateTimeFormatInfo.CurrentInfo AbbreviatedDayNames collection

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#?

like image 683
Sergey Avatar asked Jul 14 '11 11:07

Sergey


3 Answers

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

like image 99
V4Vendetta Avatar answered Oct 31 '22 19:10

V4Vendetta


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));
like image 2
Ondra Avatar answered Oct 31 '22 18:10

Ondra


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.

like image 1
Schroedingers Cat Avatar answered Oct 31 '22 18:10

Schroedingers Cat