首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DateTime.Now持续时间+- 2小时?

DateTime.Now持续时间+- 2小时?
EN

Stack Overflow用户
提问于 2011-08-14 12:59:05
回答 1查看 675关注 0票数 2

我正在尝试显示基于日期和小时+- 2小时的XML列表。

我目前正在使用这种方法:

代码语言:javascript
复制
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也没有任何相关的例子或信息。

代码语言:javascript
复制
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);



        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-14 13:04:11

使用这个

代码语言:javascript
复制
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) );
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7055105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档