C# 判斷時間段是否相交的實現(xiàn)方法
更新時間:2017年10月25日 15:17:25 作者:_iorilan
這篇文章主要介紹了C# 判斷時間段是否相交的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下
C# 判斷時間段是否相交的實現(xiàn)方法
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), Query<TCollection>.LTE(fromExp, From)); var rangeQuery = Query.Or(rangeTo, rangeFrom, Query.And(Query<TCollection>.GTE(fromExp, From),Query<TCollection>.LTE(toExp, To))); return rangeQuery; }
其中From和To為兩個時間屬性
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C#調(diào)用WinRar執(zhí)行rar、zip壓縮的方法
這篇文章主要介紹了C#調(diào)用WinRar執(zhí)行rar、zip壓縮的方法,涉及C#針對winrar的判斷與調(diào)用技巧,需要的朋友可以參考下2015-05-05c#橋接模式(bridge結(jié)構(gòu)模式)用法實例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細的分析了橋接模式的原理與用法實例,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能
這篇文章介紹了C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10