简体   繁体   English

如何从当年的特定日期找到日期名称?

[英]how to find day name from given date for current year?

I have the birth date from particular year, i want to find the day name of that birth date for current year. 我有特定年份的出生日期,我想找到当年出生日期的日期名称。

how to do this? 这个怎么做?

You can find birthdate in this year like; 你可以在今年找到生日吗?

var birthDate = new DateTime(1983, 10, 21);
var thisYear = new DateTime(DateTime.Today.Year, birthDate.Month, birthDate.Day);

var dayOfWeek = thisYear.DayOfWeek; // it gives you day of week of birth day in this year

只需使用DateTime.DayOfWeek

采用

    string day = DateTime.Now.DayOfWeek.ToString();

I use this Extension Method: 我使用这个扩展方法:

public static string GetDayName(this DateTime date)
{
    string _ret = string.Empty; //Only for .NET Framework 4.0++
    var culture = new System.Globalization.CultureInfo("en-US"); //<- '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;
}

Then we call it: 然后我们称之为:

var birthDate = new DateTime(1983, 10, 21);
var thisYear = new DateTime(DateTime.Today.Year, birthDate.Month, birthDate.Day);

var dayOfWeek = thisYear.GetDayName(); //<- Returns 'Monday'

try this 尝试这个

 string Name = CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);

Hops its helps 啤酒花有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM