简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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