1

Given a DateTime, I want to check if the .Day is between a number, say between 15 and 19.

So 9/16/2013 passes, but 9/20/2013 fails.

Thanks,

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1771591
  • 87
  • 2
  • 9

6 Answers6

11

Including

var date = DateTime.Now;
if (date.Day >= 15 && date.Day <= 19)
{
        return true;
}

Excluding

var date = DateTime.Now;
if (date.Day > 15 && date.Day < 19)
{
        return true;
}

I´m not 100% sure which one is right ;)

makim
  • 3,154
  • 4
  • 30
  • 49
3

Something like this should work:

var date = DateTime.Now;
return (date.Day >= 15 && date.Day <= 19);

Since this is just a range, you can use less than or greater than operators for comparison. Also, you might use the >= or <= operators for including the start or end day.

An alternative approach based on the fact that only one predicate must be true for the date to be out of the range:

var date = DateTime.Now;
return !(date.Day < 15 || date.Day > 19)
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
2
if((DateTime.Today.Day >= 15) && (DateTime.Today.Day <= 19))
{
    //do something
}
else
    //do something else
}
damaltor
  • 51
  • 3
1

Edit: (because of the error which you can see in the picture (in comments) i wasn't able to post the answer.)

private bool IsInRange(DateTime yourDate, DateTime lowestRange, DateTime highestRange) {
   return yourDate.Day >= lowestRange.Day && yourDate.Day <= highestRange.Day; 
}
mihai
  • 2,746
  • 3
  • 35
  • 56
  • Why check both if one is already false? Quite an overcomplication. – Rotem Sep 10 '13 at 14:07
  • sorry, i cannot submit the full answer because the stackoverflow site block my method which contains the word `return` :( – mihai Sep 10 '13 at 14:07
  • Why not just `return yourDate.Day < lowestRange.Day || yourDate.Day > highestRange.Day`? I don't know why you're using the bitwise & operator. You should use the boolean || operator as Rotem points out that only one must be true. – Cameron Tinker Sep 10 '13 at 14:07
  • yes, my original method is what you pasted, but i don't know why stackoverflow doesn't allow me to post it... i'll show the snippet with the error – mihai Sep 10 '13 at 14:08
  • ![stackoverflow validates as error in code][1] [1]: http://i.stack.imgur.com/KF6dn.jpg – mihai Sep 10 '13 at 14:10
  • Ah, the dreaded can't post answer error. I'm not exactly sure why this happens, but I feel your pain. Also, you're almost correct. If you're going to use the boolean `||` operator, you need to change it to my original comment. – Cameron Tinker Sep 10 '13 at 14:16
  • cannot post `&&` toghether :( – mihai Sep 10 '13 at 14:25
0

You can use Day property directly to check if your date is within some range using code from other answers.

MSDN says that

Starting with the .NET Framework 2.0, the DateTime structure contains a 64-bit field composed of a private Kind field concatenated with the Ticks field.

Therefore, the most efficient way to compare DateTime instances is to use Ticks property:

DateTime dateToValidate = SomeDate();
DateTime start = SomeRangeStart();
DateTime end = SomeRangeEnd();

return start.Ticks <= dateToValidate.Ticks && dateToValidate.Ticks <= end.Ticks;
takemyoxygen
  • 4,294
  • 22
  • 19
-1

To properly test if a date (day) is between given numbers, you must use the entire date, or the formula will fail if the date reaches the end of the month.

The example below checks the margin by adding days (or subtracticting days) by using AddDate to greate the boundary date. Then check if the current date is between the boundaries.

var now = DateTime.Now;
if(date.AddDays(-5) < now && now < date.AddDays(5))
{
   return true;
}
Rob van der Veer
  • 1,148
  • 1
  • 7
  • 20
  • 1
    -1 What does this have to do with anything? – Rotem Sep 10 '13 at 14:01
  • @Rotern. All you people are quick to downvote, I am trying to explain that just checking the `.Day` is not sufficient, if the date you are trying to test is close to a months start or end. Therefore you must test if the entire date is between to dates. – Rob van der Veer Sep 10 '13 at 14:03
  • That really wasn't the question. Nowhere does it say anything about `Now`. – Rotem Sep 10 '13 at 14:05
  • @Rotem the original question is about a date? Otherwise why would OP mention two dates? Anyway, my guess is that OP will eventually find out. – Rob van der Veer Sep 10 '13 at 14:06
  • The question is about determining if a date falls between two other dates. Not if a date is +-5 days from now. – Rotem Sep 10 '13 at 14:10
  • I give up. If people don't appreciate me here, i'm off. – Rob van der Veer Sep 10 '13 at 14:11
  • 3
    I downvoted an answer I thought was wrong. I gave feedback on that downvote because that is a site guideline. You shouldn't be taking it personally. – Rotem Sep 10 '13 at 14:13
  • +1, For the idea, I guess the OP would have considered month as well, but since its not specified in the question, its upto OP to clarify. – Habib Sep 10 '13 at 14:31