我有来自用户输入的 timezoneid、DayOfWeek(Sunday. Monday..) 和 Hour(int)。
如何在 .net 中获取最近的未来 DateTime 对象?
代码未经测试,但你会明白:
DayOfWeek _userDOW = DayOfWeek.Sunday;
int _userHOUR = 7;
DateTime _dt = new DateTime.Now;
while (_dt.DayOfWeek != _userDOW) {
_dt = _dt.AddDays(1);
}
// remove hours, mins and secs
_dt = new DateTime(_dt.Year, _dt.Minute, _dt.Second);
// add user hour
_dt = _dt.AddHours(_userHOUR);