How to compute interval of days between two dates in Visual Basic

Compute Interval of Days
Compute Interval of Days

This tutorial will guide us on how to create a simple program that computes the interval of days between two dates in Visual Basic 6.

Here are the procedures:

1. Open your visual basic 6 program and create a new project and select Standard EXE.

2. It so happen that by creating a new project, a form is already been added. We don’t have to add another form, just rename the form into ComputeDaysIntervalFrm.

3. Save the project and the form in the same directory or folder.

4.In this project, we need the DTPicker control to be used as the date selector. We must add first the Microsoft Windows Common Controls-2 6.0 (SP6) in the components of our visual basic project. Go to Project in the menu bar of Visual Basic and select Components, a dialog box will appear. Kindly scroll down and look for the Microsoft Windows Common Controls-2 6.0 (SP6) check the component and click OK.  

5. After we have added the controls to our project, kindly drag the DTPicker control dtPicker  in our form. We need two DTPicker control, rename the first DTPicker control into dtDateFrom and the second one into dtDateTo. We also need a Command Button, name it as cmdComputeInterval with the caption of (Compute Day(s) Interval) or any caption you want, the command button will trigger the computation of day interval between two dates. To display the difference between two dates, we need a textbox, kindly create one and name it as txtAge.

The form should be look like this:

Interval Form

You are also free to design your form.

6. Double click the command button (cmdComputeInterval) and insert the code below.

txtAge.Text = DateDiff("d", dtDateFrom.Value, dtDateTo.Value) & " days"

7. The code above should have been inserted in the click event of the command button. Just to make sure, it should be like this:

Private Sub cmdComputeInterval_Click()
txtAge.Text = DateDiff("d", dtDateFrom.Value, dtDateTo.Value) & " days"
End Sub

As we have mentioned earlier, the command button will trigger the computation.

DateDiff is a function in visual basic that will get the interval between two dates (years, months, days, weeks, hours,minute, seconds). Since our program will determine the interval of days, we used “d” in the DateDiff argument.The code states that it will deduct the value of dtDateTo from the value of dtDateFrom to get the interval of days and display it in the textbox.

8. Press F5 to test the project.

We hope that you have learned something from our tutorial and thank you for visiting inettutor.com.

, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.