Solved

VB.Net Establish if a date falls between two other dates

Posted on 2015-02-06
8
151 Views
Last Modified: 2015-02-07
I am trying to establish if a date falls between two dates including the start and finish dates

Can someone please explain why this code doesn't work

If TargetDate  >=  StartDate  AndAlso  TargetDate  <=  EndDate  Then
            MessageBox.Show("True")
Else
            MessageBox.Show("false")
End If

Open in new window


It works correctly if the StartDate and The EndDate are dates before and after the target date, but if either the StartDate or the EndDate is the same as the TargetDate it fails.   It also fails if the dates are all the same.

I need to be able to include both the Start and End Dates and I would have thought that the = sign would have been sufficient.

Many Thanks in advance for any assistance
0
Comment
Question by:PNRT
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
A DateTime instance has a Time associated with it.  Make sure your start StartDate has 0000 hrs in it and your EndDate has 2359 hrs.
0
 
LVL 1

Author Comment

by:PNRT
Comment Utility
Hi, I have declared all variables as Date, would that still have a time element?
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
No Date has not time in it. try


If (TargetDate  >=  StartDate  AndAlso  TargetDate  <=  EndDate) And (IsDate(TargetDate)  and  IsDate(EndDate))Then
            MessageBox.Show("True")
Elsehttp://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28612150.html#
            MessageBox.Show("false")
End If

Open in new window


I think the reason it is not working is possibly the data is bad and doesn't translate to date or is null. That is why I added IsDate(0 to make sure.
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
Comment Utility
Even with a time IsDate will still be true. Use targetdate.date, StartDate.date and enddate.date to get just the date part
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Sorry to contradict eghtebas, but the Date type always has a time element. The underlying type in the framework is more properly called a datetime.

If you assign a date without specifying the time, it defaults to midnight.

But too many programmers assign a Date value through the Now function. Now includes the time. So if you use Now at different times in the day, you end up with dates (datetime under the hood) that might seem to be the same when you display them with methods that do now display the time, such as ToShortDateString. But the time is there anyway, and because it is different, = does not work.

When the time component is not important, never use Now, because you end up with your problem. Use Date.Today, which always record the date at midnight, so the time is not significant when you compare two dates.

This is not only in .NET, the same thing was True in VB6. Many databases also record the date and time in a Date field, so you have to be careful about using the right functions when you assign a date where the time is not important.

If you have no control about the way the data is entered, or if you work with old data, you can always use StartDate.Date and EndDate.Date for your comparison. This uses only the date component, not matter what time was recorded.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
James,

Thank you for the correction. In T-SQL it has no time. Now I am learning from a great teacher like you vb.net is different. see:

https://msdn.microsoft.com/en-us/library/bb630352.aspx
date <-- SQL Server data type:
YYYY-MM-DD  <-- Default string literal format passed to down-level client

Is my take correct?

Mike
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
egthebas

I am not a "great teacher", I am an "old programmer", who have seen and lived all the changes through the years. And I am not a teacher, I am a technical trainer. Not just words, there are a lot of differences.

You are OK for SQL Server, but the question was about VB.NET. A Date is not a date.

And the date and time fields are a relatively new thing in T-SQL. They appeared in 2008. Before that, we had only a few datetime field types. A lot of database in use today were built before that, or by programmers who did not learn of the new types. So I still see a lot of bad uses of dates.

Access, C and C++, VB6 and COM, .NET and old versions of SQL Server all record the date and time together.
0
 
LVL 1

Author Closing Comment

by:PNRT
Comment Utility
Hi Experts
All the comments about time always being part of a date may be true, but TargetDate.Date, StartDate.Date and EndDate.Date obviously is
only the date as this seems to work fine.   Thanks to all.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now