1. 判断两个起止时间是否相交:
public static bool IsTimeBetween(TimeSpan input, TimeSpan start, TimeSpan end, bool fromInclusice, bool toInclusive)
{
//http://www.php.cn/
// 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# 判断时间段是否相交的内容,更多相关内容请关注PHP中文网(www.php.cn)!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号