Getting the Time Interval in VB.net

0
759

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.

 

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:

 

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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