开发者

DateTime.Now Duration +- 2 Hours?

I am trying to display XML listings based on Date and Hour +- 2 hours.

I am currently using this method:

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 ;



        }

But I would like to change this to select listings from my XML within 2 hours of current hour.

I have tried variations of the following, but dont know enough about DateTime and was unable to find any related questions on this site and DateTime from MSDN also didn't have any related examples or information.

bool My开发者_如何学编程DateCheckingMethod(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);



        }


use this

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) );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜