Getting a short day name

2020-06-30 08:58发布

I was wondering on how to write a method that will return me a string which will contain the short day name, example:

    public static string GetShortDayName(DayOfWeek day)

now if i call:

        string monday = GetShortDayName(DayOfWeek.Monday);

I will get back "mo" if culture is en, or "lu" if culture is at example it.

5条回答
三岁会撩人
2楼-- · 2020-06-30 09:27

try:

CultureInfo english = new CultureInfo("en-US");
string sunday = (english.DateTimeFormat.DayNames[(int)DayOfWeek.Sunday]).Substring(0, 2);

Or:

dateTimeFormats = new CultureInfo("en-US").DateTimeFormat;
string sunday = (dateValue.ToString("dddd", dateTimeFormats)).Substring(0, 2);
查看更多
何必那么认真
3楼-- · 2020-06-30 09:30

You can use "ddd" in in a custom format string to get the short day name. For example.

DateTime.Now.ToString("ddd");

As suggestby @Loudenvier in comments.

查看更多
我命由我不由天
4楼-- · 2020-06-30 09:50
DateTimeFormatInfo.ShortestDayNames

Gets or sets a string array of the shortest unique abbreviated day names associated with the current DateTimeFormatInfo object.

查看更多
smile是对你的礼貌
5楼-- · 2020-06-30 09:51

You can use DateTimeFormatInfo.AbbreviatedDayNames. For example:

string[] names = culture.DateTimeFormat.AbbreviatedDayNames;
string monday = names[(int) DayOfWeek.Monday];
查看更多
放荡不羁爱自由
6楼-- · 2020-06-30 09:51

The closest you can get is use a custom date and time format string - specifically ddd.

This will return an abbreviation - you can substring the result to get to 2 characters.

You will need to use a DateTime with a day corresponding to the day of week you wish.

查看更多
登录 后发表回答