仅当我具有字符串日期名称时,我才需要获取日期,到目前为止,我已经尝试过了
var now = DateTime.Now.Date;
var currentDay = now.DayOfWeek;
int days = (int)currentDay+1;
dt1 = now.AddDays(-days);
dt1 = dt1.AddHours(hour);
例如“星期二”。获取今天的公元前星期二是星期二
但是没有运气
答案 0 :(得分:2)
基于
C#: DateTime.DayOfWeek to string comparison - Stack Overflow
c# - How can I get the DateTime for the start of the week? - Stack Overflow
DateTime getDate(string dayOfWeekString)
{
var dayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeekString);
var now = DateTime.Now.Date;
var diff = (7 + (now.DayOfWeek - dayOfWeek)) % 7;
return now.AddDays(-1 * diff).Date;
}
Console.WriteLine(getDate("Tuesday"));
Console.ReadLine();