C# 判斷時間段是否相交的實(shí)現(xiàn)方法
更新時間:2017年10月25日 15:17:25 作者:_iorilan
這篇文章主要介紹了C# 判斷時間段是否相交的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
C# 判斷時間段是否相交的實(shí)現(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. 傳入起止時間的表達(dá)式,判斷與已知時間段的交集,生成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# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟
這篇文章主要介紹了C# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12
C#創(chuàng)建自定義控件及添加自定義屬性和事件使用實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于C#創(chuàng)建自定義控件及添加自定義屬性和事件使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
C#設(shè)計(jì)模式之Visitor訪問者模式解決長隆歡樂世界問題實(shí)例
這篇文章主要介紹了C#設(shè)計(jì)模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結(jié)合具體實(shí)例形式分析了C#使用訪問者模式解決長隆歡樂世界問題的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09

