C# 如何从选定的日期获取日期名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1615380/
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-06 19:24:32  来源:igfitidea点击:

How to get the day name from a selected date?

c#datetime

提问by Jonathan Escobedo

I have this : Datetime.Now();or 23/10/2009
I want this : Friday

我有这个:Datetime.Now();或者23/10/2009
我想要这个:Friday

For local date-time (GMT-5) and using Gregorian calendar.

对于本地日期时间 (GMT-5) 和使用公历。

采纳答案by brendan

//default locale
System.DateTime.Now.DayOfWeek.ToString();
//localized version
System.DateTime.Now.ToString("dddd");

To make the answer more complete:

为了使答案更完整:

回答by Letterman

DateTime.Now.DayOfWeekquite easy to guess actually.

DateTime.Now.DayOfWeek其实很容易猜到。

for any given date:

对于任何给定日期:

   DateTime dt = //....
   DayOfWeek dow = dt.DayOfWeek; //enum
   string str = dow.ToString(); //string

回答by Joseph

You're looking for the DayOfWeek property.

您正在寻找 DayOfWeek 属性。

Here's the msdn article.

这是msdn 文章

回答by Charles Bretana

DateTime now = DateTime.Now
string s = now.DayOfWeek.ToString();

回答by Fredrik M?rk

If you want to know the day of the week for your code to do something with it, DateTime.Now.DayOfWeekwill do the job.

如果你想知道你的代码在一周中的哪一天用它做某事,就DateTime.Now.DayOfWeek可以完成这项工作。

If you want to display the day of week to the user, DateTime.Now.ToString("dddd")will give you the localized day name, according to the current culture (MSDN info on the "dddd" format string).

如果您想向用户显示星期几,DateTime.Now.ToString("dddd")则会根据当前文化(“dddd”格式字符串上的 MSDN 信息)为您提供本地化的日期名称。

回答by Manoj Talreja

try this:

尝试这个:

DateTime.Now.DayOfWeek

回答by Mustafa Gülmez

System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.GetDayName(System.DateTime.Now.DayOfWeek)

or

或者

System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.GetDayName(DateTime.Parse("23/10/2009").DayOfWeek)

回答by Subrata Sarkar

What about if we use String.Format here

如果我们在这里使用 String.Format 呢?

DateTime today = DateTime.Today;
String.Format("{0:dd-MM}, {1:dddd}", today, today) //In dd-MM format
String.Format("{0:MM-dd}, {1:dddd}", today, today) //In MM-dd format

回答by Abhendra Tiwari

(DateTime.Parse((Eval("date").ToString()))).DayOfWeek.ToString()

at the place of Eval("date"),you can use any date...get name of day

Eval("date") 的位置,您可以使用任何日期...获取日期名称

回答by Jhollman

I use this Extension Method:

我使用这个扩展方法:

public static string GetDayName(this DateTime date)
{
    string _ret = string.Empty; //Only for .NET Framework 4++
    var culture = new System.Globalization.CultureInfo("es-419"); //<- 'es-419' = Spanish (Latin America), 'en-US' = English (United States)
    _ret = culture.DateTimeFormat.GetDayName(date.DayOfWeek); //<- Get the Name     
    _ret = culture.TextInfo.ToTitleCase(_ret.ToLower()); //<- Convert to Capital title
    return _ret;
}