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

Posted on 2015-02-06
Medium Priority
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
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
Question by:PNRT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40595020
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.

Author Comment

ID: 40595028
Hi, I have declared all variables as Date, would that still have a time element?
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40595064
No Date has not time in it. try

If (TargetDate  >=  StartDate  AndAlso  TargetDate  <=  EndDate) And (IsDate(TargetDate)  and  IsDate(EndDate))Then
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Accepted Solution

ChloesDad earned 2000 total points
ID: 40595140
Even with a time IsDate will still be true. Use targetdate.date, StartDate.date and enddate.date to get just the date part
LVL 40
ID: 40595240
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.
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40595246

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:

date <-- SQL Server data type:
YYYY-MM-DD  <-- Default string literal format passed to down-level client

Is my take correct?

LVL 40
ID: 40595268

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.

Author Closing Comment

ID: 40595383
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 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