我正在尝试使用星期几创建闹钟。
例如,如果今天是星期日,那么我的闹钟时间将设置为星期一。
我尝试了以下代码,但不是正确的代码:
DayOfWeek day = (DayOfWeek)DayOfWeek.Monday;
DateTime beginTime1 = time.DayOfWeek;
答案 0 :(得分:2)
您无法将DayOfWeek
枚举转换为Date
。你期望什么日期?
这是一种从开始日期移至一周的第二天的扩展方法:
public static DateTimeOffset AddToDayOfWeek(this DateTime start, DayOfWeek day,
bool includingToday)
{
int daysDifference = (int)start.DayOfWeek - (int)day;
if (daysDifference < 0 || (includingToday && daysDifference == 0))
{
daysDifference = 7 - daysDifference;
}
return start.AddDays(daysDifference);
}
然后你可以像这样使用它:
DateTime nextSunday = DateTime.Today.AddToDayOfWeek(DayOfWeek.Sunday, false);
答案 1 :(得分:0)
尝试:
DateTime alarm = DateTime.Now.Add(new TimeSpan(24, 0, 0));
其中24小时。
或
DateTime alarm = DateTime.Now.Add(TimeSpan.FromHours(24));