我有来自用户输入的timezoneid,DayOfWeek(星期日,星期一......)和小时(int)。
如何在.net中获取最近的DateTime对象?
答案 0 :(得分:2)
代码未经过测试,但您会明白:
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);