473,769 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ 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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);

Thanks, Mark
Nov 16 '05 #1
6 8492
Haven't tested this, so be warned...

private bool IsBetween(DateT ime startDate, DateTime endDate, DateTime date)
{
TimeSpan startSpan = startDate - date;
if(startSpan.Da ys > 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*******@disc ussions.microso ft.com> wrote in message
news:C3******** *************** ***********@mic rosoft.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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);

Thanks, Mark

Nov 16 '05 #2

private void button16_Click( object sender, System.EventArg s e)
{
DateTime givenDate = DateTime.Now;
DateTime startDate = DateTime.Now.Su btract(TimeSpan .FromDays(5));
DateTime endDate = DateTime.Now;

// Test a date inside the range.
if ( Utils.DateBetwe en(startDate, endDate, givenDate) )
Console.WriteLi ne("Date is between");
else
Console.WriteLi ne("Date is not between");

// Test with a date outside the range.
givenDate = startDate.Subtr act(TimeSpan.Fr omSeconds(1));
if ( Utils.DateBetwe en(startDate, endDate, givenDate) )
Console.WriteLi ne("Date is between");
else
Console.WriteLi ne("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(Dat eTime 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*******@disc ussions.microso ft.com> wrote in message
news:C3******** *************** ***********@mic rosoft.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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);

Thanks, Mark


Nov 16 '05 #3
MarkAurit <Ma*******@disc ussions.microso ft.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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);


if (dateToTest >= minDate && dateToTest <= maxDate)
{
....
}

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
MarkAurit <Ma*******@disc ussions.microso ft.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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);
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.co m>
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*******@disc ussions.microso ft.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(DateTi me.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=Date Time.Parse("mm/dd/yy");
maxDate=DateTim e.Now;
minDate=DateTim e.Now.AddDays(-5);
bool b1=IsBetween(mi nDate,maxDate,d ateToTest);


if (dateToTest >= minDate && dateToTest <= maxDate)
{
....
}

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

Nov 17 '05 #6
MarkAurit <Ma*******@disc ussions.microso ft.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.Da te.

--
Jon Skeet - <sk***@pobox.co m>
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
13131
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 objects be considered equal? Is there a *general* way to test for date != datetime as well as 4.5 != 4.6?
2
10213
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 document.write(mmddyy)
4
2857
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 of July 31, if I
5
1431
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 months ago, compare "the date from DB" & "the date 6 months ago from now" show the result.
3
10229
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 Date(); if(sdate < todaysdate)
4
3765
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
3805
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 of the values comes from the database, and for existing values it works fine, but if the date doesn't exist (which will always be the condition when the user first enters into the form) I am adding logic to my javascript like: if (dbDate <...
4
7418
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 date is retrieved like this: curDt = new Date();
16
4461
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 22:02:51? (24 hour day is fine) How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is 10/01/08?
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3967
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 we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.