-5

How to check if date 11/12/2014 exists in between dates '11/10/201' and '11/15/2014' ?

  • If they are `DateTime`, you can use `<` or `>` or `DateTime.Compare()` for your values. `if((11/12/201 < 11/15/2014) && (11/12/201 > 11/10/2014))` – Soner Gönül Nov 12 '14 at 09:35
  • take advantage of `DAteTime` – chouaib Nov 12 '14 at 09:36
  • one more suggestion: you can build your comparison function so easily – chouaib Nov 12 '14 at 09:36
  • You could substring each day/month/year between "/" then parse to int, compare the integers then return true or false as to whether its correct. – Paulie Nov 12 '14 at 09:39
  • @Kenyanke: That would be a *really* bad idea. Date and time handling is hard enough as it is, without performing your own parsing/formatting and unnecessary string handling. – Jon Skeet Nov 12 '14 at 09:43
  • @JonSkeet Though it wouldn't be the best idea it would be an idea, if he/she cannot get it working, it's always good to fall back on other methods that can get the job done. – Paulie Nov 12 '14 at 09:45
  • @Kenyanke: I can't imagine a situation where falling back to manually splitting and parsing would be the right answer to this problem. If a cleaner solution fails on their first attempt, the OP would be better off working at that than falling back to your suggestion. – Jon Skeet Nov 12 '14 at 09:47
  • @JonSkeet Converting Dates without using methods, requires you to split the date up, check whether the days are days, and months are months etc, that's an example where it would be used. – Paulie Nov 12 '14 at 09:51
  • @Kenyanke: Converting date/time values without using either the built-in libraries or something like my Noda Time project is pretty much always a bad idea. Why would you *want* to reinvent the wheel? – Jon Skeet Nov 12 '14 at 09:55

2 Answers2

1

The following shows an example using DateTime:

var startDate = new DateTime(2014,11,10);
var endDate = new DateTime(2014,11,15);

var dateToCheck = new DateTime(2014,11,12);

if(startDate < dateToCheck && endDate > dateToCheck)
{
    // Do something
}
Chief Wiggum
  • 2,784
  • 2
  • 31
  • 44
0
if (dateToBeChecked > lowDate && dateToBeChecked < highDate)
{
    // do this where the date is between the dates specified
}
else
{
    // do something else
}
user1666620
  • 4,800
  • 18
  • 27
  • 2
    Any time you've got `if (condition)` { return true; } else { return false; }` it's much clearer to write `return condition;`. – Jon Skeet Nov 12 '14 at 09:39
  • @JonSkeet yeah, but just wanted to be more explicit because somebody who is asking this kind of question wouldn't necessarily be all that experienced. – user1666620 Nov 12 '14 at 09:41
  • All the more reason not to provide non-idiomatic code. You could change the `return` statements to `// Code to execute if it's between the dates` and `// Code to execute otherwise` – Jon Skeet Nov 12 '14 at 09:42
  • @JonSkeet cool, will take that on board in future. – user1666620 Nov 12 '14 at 09:44