C# 判斷時(shí)間段是否相交的實(shí)現(xiàn)方法
C# 判斷時(shí)間段是否相交的實(shí)現(xiàn)方法
1. 判斷兩個(gè)起止時(shí)間是否相交:
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. 傳入起止時(shí)間的表達(dá)式,判斷與已知時(shí)間段的交集,生成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為兩個(gè)時(shí)間屬性
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
上一篇:C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)
欄 目:C#教程
下一篇:基于C#動(dòng)手實(shí)現(xiàn)網(wǎng)絡(luò)服務(wù)器Web Server
本文標(biāo)題:C# 判斷時(shí)間段是否相交的實(shí)現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5437.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)判斷當(dāng)前操作用戶管理角色的方法
 - 01-10WinForm判斷關(guān)閉事件來源于用戶點(diǎn)擊右上角“關(guān)閉”按鈕的方法
 - 01-10http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實(shí)不準(zhǔn)確、不
 - 01-10C#身份證號碼驗(yàn)證是否正確
 - 01-10C#判斷三角形的類型
 - 01-10C#中判斷一個(gè)集合是否是另一個(gè)集合的子集的簡單方法
 - 01-10C# WinForm 判斷程序是否已經(jīng)在運(yùn)行,且只允許運(yùn)行一個(gè)實(shí)例,附
 - 01-10C# 判斷字符為空的6種方法的效率實(shí)測對比
 - 01-10C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
 - 01-10C#判斷一個(gè)String是否為數(shù)字類型
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 01-10delphi制作wav文件的方法
 


