Getting the Time Interval in VB.net

Getting the Time Interval in VB.net

This tutorial is all about Getting the Time Interval in VB.net.

In this tutorial, I’m going to teach you how to get a time interval in VB.NET. You have to use the TimeSpan properties for getting the time interval of two times. With this, It will segregate the hours, minutes, seconds, millisecond and even the tick of the clock.
I used Microsoft Visual Studio 2008 for creating this application.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.

What are the most important characteristics of Visual Basic?

Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.

Let’s begin:
Open Microsoft Visual Studio, create a new Windows Form Application and name it “TimeInterval”. Set the Form just like this.
timeintervalform1
After setting up the Form, go to the solution explorer and hit the view code.
timeintervalform2
In the code view, create a method to separate the time interval to its corresponding fields.

[vbnet]
Private Sub TimeInterval(ByVal tspan As TimeSpan)
‘USED THE PROPERTIES OF THE TIMESPAN AND IT DEMONSTRATE TimeSpan.Hours
‘, TimeSpan.Milliseconds , TimeSpan.Minutes , TimeSpan.Seconds
‘ AND TimeSpan.Ticks
Try
txt_hours.Text = tspan.Hours.ToString
txt_millisecond.Text = tspan.Milliseconds.ToString
txt_minutes.Text = tspan.Minutes.ToString
txt_seconds.Text = tspan.Seconds.ToString
txt_ticks.Text = tspan.Ticks.ToString
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text)
End Try
End Sub
[/vbnet]

After creating a method, you have to convert the DateTimePicker to DateTime Function and Set the method that you have created in the click event handler of the Button to get and separate the time interval of two times.

[vbnet]
Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_go.Click
Try

‘CREATE A VARIABLE OF A TIMESPAN
Dim tspan As TimeSpan
‘CREATE THE VARIABLE OF A DATETIME
Dim strtdate As DateTime
Dim enddate As DateTime

‘SET THE CONVERTED TIME FROM THE DATETIMEPICKER
‘TO THE VARIABLE THAT YOU HAVE DECLARED
strtdate = DateTime.Parse(dtp_strTime.Text)
enddate = DateTime.Parse(dtp_Endtime.Text)
‘SET THIS FORMULA TO THE GET THE TIME INTERVAL OF TWO TIMES.
tspan = enddate.Subtract(strtdate).Duration

‘PERFORM THE SUB PROCEDURE THAT YOU HAVE
‘CREATED TO SEGRAGATES THE TIME INTERVAL THAT YOU HAVE GET.
TimeInterval(tspan)
Catch Ex As Exception
MessageBox.Show(Ex.Message, Me.Text)
End Try
End Sub
[/vbnet]

Lastly, set the value of the end time higher than the starting time on the first load of the Form.

[vbnet]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘SET THE VALUE OF THE END DATE HIGHER THAN THE START DATE
dtp_Endtime.Value = DateAdd(DateInterval.Hour, 9, dtp_Endtime.Value)
dtp_Endtime.Value = DateAdd(DateInterval.Minute, 30, dtp_Endtime.Value)
dtp_Endtime.Value = DateAdd(DateInterval.Second, 30, dtp_Endtime.Value)
End Sub
[/vbnet]

press F5 on the keyboard of you computer to run your project.
Output:

timeintervalprint

Readers might read also:

f you have any questions or suggestions about this system project or Getting the Time Interval in VB.net, just send your inquiry using our contact page or simply leave a comment below.

Leave a Comment