-1

I would to check if a date fall between two limites.

StartDate = '2018-01-01 11:10:00' EndDate = '2018-02-01 14:00:30'

  1. CheckDate1 = '2018-01-01 11:09:00' -> FAIL
  2. CheckDate2 = '2018-01-01 11:10:01' -> OK
  3. CheckDate3 = '2018-01-01 14:00:31' -> FAIL
  4. CheckDate4 = '2018-01-01 14:00:29' -> OK

I tried the following check:

CheckDateX >= StartDate && CheckDateX <= EndDate;

This doesn't seems to take the seconds into account. So I tried the following check:

 CheckDateX.Ticks >= StartDate.Ticks  && CheckDateX.Ticks <= EndDate.Ticks;

But the result is also wrong. Is there a correct and elegant way to compare to DateTime in C#?

Mikev
  • 2,012
  • 1
  • 15
  • 27
pix
  • 1,264
  • 19
  • 32

1 Answers1

0

You should check that link : How to know if a DateTime is between a DateRange in C# People provided answers and even an extension for the same issue you are facing.

Littledaem
  • 51
  • 6
  • In fact it was good without the ticks..... – pix May 08 '19 at 16:08
  • I will flag my own question as duplicate :) – pix May 08 '19 at 16:08
  • @pix check my answer for getting build time configuration from code. Somehow your question is removed because of duplicate but actually you can do it thru code. I dont have any other way to reach you. i will delete this comment after you read it. https://stackoverflow.com/questions/829276/how-to-obtain-build-configuration-at-runtime/56295835#56295835 – Jeeva Subburaj May 24 '19 at 16:12
  • Thx @JeevaSubburaj, my post as been deleted because the community requested it. This is really close to want I wanna do, but not enaught. I do not want to have to hard code anything. The best think would be to list all the preprocessor values inside a List variable. I really appreciate your concerne on this question :) – pix May 27 '19 at 06:23