C# 判断时间段是否相交的实现方法

C# 判断时间段是否相交的实现方法,第1张

概述C#判断时间段是否相交的实现方法1.判断两个起止时间是否相交:publicstaticboolIsTimeBetween(TimeSpaninput,TimeSpanstart,TimeSpanend,boolfromInclusice,booltoInclusive)

C# 判断时间段是否相交的实现方法

1. 判断两个起止时间是否相交:

public static bool IsTimeBetween(TimeSpan input,TimeSpan start,TimeSpan end,bool fromInclusice,bool toInclusive)     {       //http://stackoverflow.com/questions/592248/how-can-i-check-if-the-current-time-is-between-in-a-time-frame       // see if start comes before end       if (end < start)       {         return           ((toInclusive && (input <= end)) || (!toInclusive && (input < end)))           ||           ((fromInclusice && (input >= start)) || (!fromInclusice && (input > start)));       }       else       {         return           ((fromInclusice && (input >= start)) || (!fromInclusice && (input > start)))           &&           ((toInclusive && (input <= end)) || (!toInclusive && (input < end)));       }       } 

2. 传入起止时间的表达式,判断与已知时间段的交集,生成Mongo查询:

public IMongoquery GetMongoqueryIntersectWith<TCollection>(       Expression<Func<TCollection,DateTime>> fromExp,Expression<Func<TCollection,DateTime>> toExp)     {       var rangeto = query.And(query<TCollection>.GTE(toExp,To),query<TCollection>.LTE(fromExp,To));       var rangeFrom = query.And(query<TCollection>.GTE(toExp,From),From));        var rangequery = query.Or(rangeto,rangeFrom,query.And(query<TCollection>.GTE(fromExp,query<TCollection>.LTE(toExp,To)));       return rangequery;     } 

其中From和To为两个时间属性

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的C# 判断时间段是否相交的实现方法全部内容,希望文章能够帮你解决C# 判断时间段是否相交的实现方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存