开发者

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 wan开发者_StackOverflow中文版t to see Tue.

Is it possible in C#?


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


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));


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜