这篇文章主要介绍了C# 判断时间段是否相交的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
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), 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为两个时间属性
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
织梦狗教程
本文标题为:C# 判断时间段是否相交的实现方法


基础教程推荐
猜你喜欢
- C# 解析XML和反序列化的示例 2023-04-14
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity shader实现高斯模糊效果 2023-01-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中的Linq to JSON操作详解 2023-06-08