我正在尝试显示基于日期和小时+- 2小时的XML列表。
我目前正在使用这种方法:
bool MyDateCheckingMethod(string dateString)
{
DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
// Is this today (Checking Date and Hour)
return otherDate.Hour == DateTime.UtcNow.Hour &&
otherDate.Date == DateTime.UtcNow.Date ;
}但我想将其更改为在当前小时内从我的XML中选择列表。
我已经尝试了以下的变体,但对DateTime了解不够多,在这个网站上找不到任何相关的问题,MSDN的DateTime也没有任何相关的例子或信息。
bool MyDateCheckingMethod(string dateString)
{
DateTime now = DateTime.UtcNow;
DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
// Is this today (Checking Date and Hour)
return otherDate.Hour == DateTime.UtcNow.Hour &&
otherDate.Date == DateTime.UtcNow.Date ;
(otherDate - now).Duration <= TimeSpan.FromHours(2);
}发布于 2011-08-14 13:04:11
使用这个
bool MyDateCheckingMethod(string dateString)
{
DateTime now = DateTime.UtcNow;
DateTime otherDate = DateTime.ParseExact(dateString, "yyyyMMddHHmmss K", null);
return ( now.AddHours (-2) <= otherDate && otherDate <= now.AddHours (2) );
}https://stackoverflow.com/questions/7055105
复制相似问题