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

时间:2009-10-23 19:16:09

标签: c# datetime

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

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

13 个答案:

答案 0 :(得分:88)

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

使答案更完整:

答案 1 :(得分:14)

如果您想知道代码中的某一天要对其执行某些操作,DateTime.Now.DayOfWeek将完成此任务。

如果您想向用户显示星期几,DateTime.Now.ToString("dddd")会根据当前文化(MSDN info on the "dddd" format string)为您提供本地化的日期名称。

答案 2 :(得分:9)

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

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

答案 3 :(得分:6)

实际上很容易猜到

DateTime.Now.DayOfWeek

任何指定日期:

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

答案 4 :(得分:4)

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

答案 5 :(得分:3)

试试这个:

DateTime.Now.DayOfWeek

答案 6 :(得分:2)

您正在寻找DayOfWeek酒店。

这是msdn article

答案 7 :(得分:0)

如果我们在这里使用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

答案 8 :(得分:0)

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

Eval("日期")的地方,您可以使用任何日期...获取当天的名称

答案 9 :(得分:0)

我使用此扩展方法:

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

答案 10 :(得分:0)

        Random Rnd = new Random();
        RandomDates Rdate = new RandomDates();
        PaymentDetails Payd = new PaymentDetails();
        DayOfWeek strDay = DateTime.Today.DayOfWeek;
        var dateTime = DateTime.Now;
        var dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");


        StepDescription = "Fill MatterInformation. ";

        
       

            Console.Write(" Input the Day : ");
            dt = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Input the Month : ");
            mn = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Input the Year : ");
            yr = Convert.ToInt32(Console.ReadLine());
            DateTime d = new DateTime(2021, 04, yr);
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var diff = d.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            var x = d.AddDays(-diff).Date;


            dateTime = DateTime.Now;
            dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
            Console.WriteLine($"Date Value: {dateValue2}");

           // strDay = 

        }
        if (!strDay.Equals("Sunday") | !strDay.Equals("Saturday"))
        {
            Console.WriteLine("___________________OK____________________________________________");
            // string NotificateionDate = Rdate.DateWithin_PastDays(Rnd.Next(30, 260)).ToString(@"MM\/dd\/yyyy");

            // CustomLibrary.seWaitUntilElementIsVisible(10, NotiFiedDateTab.Actions.seGetLocator(), "NotiFiedDateTab");
            NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);

        }
        else
        {
            Console.WriteLine("_________________________NOT______________________________________");
            if (strDay.Equals("Sunday"))
            {
                dateTime = dateTime.AddDays(-2);
                dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
                NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);
            }
            else if (strDay.Equals("Saturday"))
            {

                dateTime = dateTime.AddDays(-1);
                dateValue2 = dateTime.ToString(@"MM\/dd\/yyyy");
                NotiFiedDateTab.Actions.Set(ControlPropertyNames.Text, dateValue2);
            }
        }
      
    }

答案 11 :(得分:0)

这里更简单

DateTime dt;
string yourdate = dt.DayOfWeek.ToString()

总比宣布多余的 DayOfWeek 好

答案 12 :(得分:-2)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessTheDay
{
class Program
{
    static void Main(string[] args)
    { 
 Console.WriteLine("Enter the Day Number ");
 int day = int.Parse(Console.ReadLine());
 Console.WriteLine(" Enter The Month");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Year ");
int year = int.Parse(Console.ReadLine());
DateTime mydate = new DateTime(year,month,day);
string formatteddate = string.Format("{0:dddd}", mydate);
Console.WriteLine("The day should be " + formatteddate);
}  
 } 
   }