欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-18 09:13:04
c# 判断时间段是否相交的实现方法 1. 判断两个起止时间是否相交: public static bool istimebetween(timespan i...

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

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

public static bool istimebetween(timespan input, timespan start, timespan end, bool frominclusice, bool toinclusive) 
    { 
      //http://*.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为两个时间属性

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

上一篇:

下一篇: