Getting a short day name

前端 未结 5 2298
一生所求
一生所求 2021-02-18 13:51

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(Da         


        
相关标签:
5条回答
  • 2021-02-18 14:05

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

    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.

    0 讨论(0)
  • 2021-02-18 14:10

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

    DateTime.Now.ToString("ffffd");
    

    As suggestby @Loudenvier in comments.

    0 讨论(0)
  • 2021-02-18 14:12

    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("ffffdd", dateTimeFormats)).Substring(0, 2);
    
    0 讨论(0)
  • 2021-02-18 14:18
    DateTimeFormatInfo.ShortestDayNames
    

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

    0 讨论(0)
  • 2021-02-18 14:20

    You can use DateTimeFormatInfo.AbbreviatedDayNames. For example:

    string[] names = culture.DateTimeFormat.AbbreviatedDayNames;
    string monday = names[(int) DayOfWeek.Monday];
    
    0 讨论(0)
提交回复
热议问题