Software Development

Determine the number of days between dates in VB.NET

Trying to figure out how many days there are between two given dates? Find out why you may want to use VB.NET's <i>TimeSpan</i> object.

Developers often deal with various date manipulations in applications. If you need a simple way to figure out how many days there are between two given dates, consider using VB.NET's TimeSpan object.

Listing A shows how you can use the TimeSpan object to obtain the elapsed amount of time between two dates. Then you will use the Subtract method to find the actual number of days between the two dates.

Notes about the example

I set a start date with a Date variable, dtStartDate, and set its value to January 1, 2007. I also declared other variables I will use, including: tsTimeSpan as a TimeSpan object, iNumberOfDays as an Integer, and strMsgText as a String. I set the value of the tsTimeSpan variable to the difference between current date/time and the value of dtStartDate.

In order to calculate the number of days between current date/time (Now) and the dtStartDate, I use the TimeSpan object's Days property and set its value to iNumberOfDays. Then I create a text that I will show in a message box by concatenating necessary text and variables in setting the value for strMsgText. The last step is displaying the value of strMsgText in a Message box.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

8 comments
billybraga
billybraga

(t1 - t2).Days (where t1 and t2 are Date)

gretchen
gretchen

Just what I needed! Thanks!

ChuckSomerville
ChuckSomerville

As usual for many of these .NET tips, the technique illustrated has a VB6 equivalent function "baked in". if t1 and t2 are strings containing valid date expressions, then this... td = DateDiff("d", t1, t2) ...returns the difference in days between t1 and t2. It's days because the first parameter is "d". You can have years, months, hours, minutes, or whatever interval size you want. See the VB6 help for DateDiff() to see valid values. For example... td = DateDiff("d", "1/1/2007", "2007 March 23") ...returns 81 (which is correct). Note that about any recognizable date format will do. Couldn't really be any easier. Chuck Somerville Dayton Ohio

moira
moira

This didn't work for me - it had a problem with "timespan" and gave the error "type expected". The code just wouldn't run, even though timespan seemed to be an option, ie it was in the automatic list that pops up.

joemach
joemach

Thanks for that bit of info on using DateDiff Function. That will be handy.

lsweeke
lsweeke

Moira, the problem is that you cannot declare the TimeSpan as given in the code. I encountered the same problem as you did(i.e. the "type expected" error msg). This works fine for me... Dim tsTimeSpan As New System.TimeSpan If you change to the above declaration, it should work. All the best ;)

moira
moira

I tried what you suggested and though it appeared to accept the code, ie no zig zag underlining of words occurred, running it produced the message that there were build errors and when I chose to continue anyway it said something about not finding timespan.exe.

Editor's Picks