Rumah > pembangunan bahagian belakang > Tutorial C#.Net > C# 判断时间段是否相交

C# 判断时间段是否相交

黄舟
Lepaskan: 2017-03-01 10:42:25
asal
3057 orang telah melayarinya

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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)));

            }

 

 

        }

Salin selepas log masuk

2. 传入起止时间的表达式,判断与已知时间段的交集,生成Mongo查询:

1

2

3

4

5

6

7

8

9

10

11

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;

        }

Salin selepas log masuk


其中From和To为两个时间属性

 以上就是C# 判断时间段是否相交的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan