Solved

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

Posted on 2015-02-06
8
154 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
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.
0
 
LVL 2

Author Comment

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

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
            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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 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
0
 
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.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40595246
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
ID: 40595268
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 2

Author Closing Comment

by:PNRT
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

806 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