C# 获得一个简短的日名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12530116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:35:22  来源:igfitidea点击:

Getting a short day name

c#datetime-format

提问by MaiOM

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.

如果文化是 en,我会找回“mo”,如果文化是例子,我会找回“lu”。

采纳答案by Jon Skeet

You can use DateTimeFormatInfo.AbbreviatedDayNames. For example:

您可以使用DateTimeFormatInfo.AbbreviatedDayNames. 例如:

string[] names = culture.DateTimeFormat.AbbreviatedDayNames;
string monday = names[(int) DayOfWeek.Monday];

回答by Oded

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

最接近的是使用自定义日期和时间格式字符串- 特别是ddd

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

这将返回一个缩写 - 您可以将结果子串化为 2 个字符。

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

您需要将 aDateTime与您希望的星期几对应。

回答by CloudyMarble

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

回答by dedpichto

DateTimeFormatInfo.ShortestDayNames

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

获取或设置与当前 DateTimeFormatInfo 对象关联的最短唯一缩写日名称的字符串数组。

回答by mcNux

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

您可以在自定义格式字符串中使用“ddd”来获取短日期名称。例如。

DateTime.Now.ToString("ddd");

As suggestby @Loudenvier in comments.

正如@Loudenvier 在评论中所建议的那样。