473,480 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Date Comparison Question

Im having difficulty coming up with a good algorithm to express the following
comparison:

"if <a given date> 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 #1
6 8476
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" <Ma*******@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Im having difficulty coming up with a good algorithm to express the
following
comparison:

"if <a given date> 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

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

/// <summary>
/// Returns true if date is between start and end date inclusive. Put in
some static class, etc.
/// </summary>
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" <Ma*******@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Im having difficulty coming up with a good algorithm to express the following comparison:

"if <a given date> 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
MarkAurit <Ma*******@discussions.microsoft.com> wrote:
Im having difficulty coming up with a good algorithm to express the following
comparison:

"if <a given date> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
MarkAurit <Ma*******@discussions.microsoft.com> wrote:
Im having difficulty coming up with a good algorithm to express the
following
comparison:

"if <a given date> 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)
{
...
}


Ohhhh bugger! There I was happily playing around with op_Subtract and
TimeSpans and I completely forgot that DateTime supported the comparison
operators...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
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 <Ma*******@discussions.microsoft.com> wrote:
Im having difficulty coming up with a good algorithm to express the following
comparison:

"if <a given date> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
MarkAurit <Ma*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
13098
by: Donnal Walter | last post by:
I was very surprised to discover that >>> import datetime >>> x = datetime.date(2004, 9, 14) >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15) >>> print x == y True How can these two...
2
10184
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
4
2824
by: ianv2 | last post by:
Hi Is the following possible using Javascript ? I would like a page to redirect to another page if the page expiry has passed. E.G. If my questionnaireform.html page had an expiry date...
5
1412
by: Dinçer | last post by:
I need to compare dates. But this way: I get a date from database. I need to understand if this date is newer then "6 months" or not. What I need is get the current date, calculate the date 6...
3
10209
by: Tiya | last post by:
Hi there !!! I would like to know how to compare dates in javascript. var sdate = new Date(theform.SubmissionDate.value); var odate = new Date(theform.StartDate.value); var todaysdate = new...
4
3732
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
5
3783
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
4
7386
by: anagai | last post by:
I just want to check if a date entered in a textbox is equal to the current system date. I set the date object from the input field like this: dt1=new Date('10/01/2007'); the current system...
16
4428
by: W. eWatson | last post by:
Are there some date and time comparison functions that would compare, say, Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd) Is 02/11/07 the same as 02/11/07? Is 14:05:18 after...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.