459,273 Members | 1,037 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

# Date Comparison Question

6 Replies

 P: n/a Haven't tested this, so be warned... private bool IsBetween(DateTime startDate, DateTime endDate, DateTime date) { TimeSpan startSpan = startDate - date; if(startSpan.Days > 0) { // Dunno if you want to use days or something finer grained // Also not sure if you want inclusive or not TimeSpan endSpan = endDate - date; if(endSpan.Days > 0) { return true; } } return false; } "MarkAurit" wrote in message news:C3**********************************@microsof t.com... Im having difficulty coming up with a good algorithm to express the following comparison: "if falls between the (current date - 5 days) and the (current date)" Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used for the inner and outer ranges, its how to express the "between" that has me. /* what Id like to do, in pseudo code */ dateToTest=DateTime.Parse("mm/dd/yy"); maxDate=DateTime.Now; minDate=DateTime.Now.AddDays(-5); bool b1=IsBetween(minDate,maxDate,dateToTest); Thanks, Mark Nov 16 '05 #2

 P: n/a private void button16_Click(object sender, System.EventArgs e) { DateTime givenDate = DateTime.Now; DateTime startDate = DateTime.Now.Subtract(TimeSpan.FromDays(5)); DateTime endDate = DateTime.Now; // Test a date inside the range. if ( Utils.DateBetween(startDate, endDate, givenDate) ) Console.WriteLine("Date is between"); else Console.WriteLine("Date is not between"); // Test with a date outside the range. givenDate = startDate.Subtract(TimeSpan.FromSeconds(1)); if ( Utils.DateBetween(startDate, endDate, givenDate) ) Console.WriteLine("Date is between"); else Console.WriteLine("Date is not between"); } /// /// Returns true if date is between start and end date inclusive. Put in some static class, etc. /// public static bool DateBetween(DateTime start, DateTime end, DateTime date) { if ( date >= start && date <= end ) return true; return false; } // Output Date is between Date is not between -- William Stacey, MVP http://mvp.support.microsoft.com "MarkAurit" wrote in message news:C3**********************************@microsof t.com... Im having difficulty coming up with a good algorithm to express the following comparison: "if falls between the (current date - 5 days) and the (current date)" Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used for the inner and outer ranges, its how to express the "between" that has me. /* what Id like to do, in pseudo code */ dateToTest=DateTime.Parse("mm/dd/yy"); maxDate=DateTime.Now; minDate=DateTime.Now.AddDays(-5); bool b1=IsBetween(minDate,maxDate,dateToTest); Thanks, Mark Nov 16 '05 #3

 P: n/a Thanks, Jon. This fixes that issues of comparison. However, my problem is more subtle: even if the date is the same, the test isnt correctly working because the time isnt the same. Im taking a date which comes from a business object where its defined as a string - even if its todays date, when I perform a DateTime.Parse on it and equality comparing it to DateTime.Now, the test is false. So: I need to compare only the date part of a DateTime, and I have to test for greater than (if its was always equality, I could ToDateString()). Any help is hugely appreciated. "Jon Skeet [C# MVP]" wrote: MarkAurit wrote: Im having difficulty coming up with a good algorithm to express the following comparison: "if falls between the (current date - 5 days) and the (current date)" Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used for the inner and outer ranges, its how to express the "between" that has me. /* what Id like to do, in pseudo code */ dateToTest=DateTime.Parse("mm/dd/yy"); maxDate=DateTime.Now; minDate=DateTime.Now.AddDays(-5); bool b1=IsBetween(minDate,maxDate,dateToTest); if (dateToTest >= minDate && dateToTest <= maxDate) { .... } -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 17 '05 #6

 P: n/a MarkAurit wrote: This fixes that issues of comparison. However, my problem is more subtle: even if the date is the same, the test isnt correctly working because the time isnt the same. Im taking a date which comes from a business object where its defined as a string - even if its todays date, when I perform a DateTime.Parse on it and equality comparing it to DateTime.Now, the test is false. So: I need to compare only the date part of a DateTime, and I have to test for greater than (if its was always equality, I could ToDateString()). Any help is hugely appreciated. Just use the Date property of DateTime to get a DateTime with an empty time portion. You can use DateTime.Today to get the equivalent of DateTime.Now.Date. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 17 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.