Getting the Time Interval in VB.net

0
394

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.

  1. Private Sub TimeInterval(ByVal tspan As TimeSpan)
  2. 'USED THE PROPERTIES OF THE TIMESPAN AND IT DEMONSTRATE TimeSpan.Hours
  3. ', TimeSpan.Milliseconds , TimeSpan.Minutes , TimeSpan.Seconds
  4. ' AND TimeSpan.Ticks
  5. Try
  6. txt_hours.Text = tspan.Hours.ToString
  7. txt_millisecond.Text = tspan.Milliseconds.ToString
  8. txt_minutes.Text = tspan.Minutes.ToString
  9. txt_seconds.Text = tspan.Seconds.ToString
  10. txt_ticks.Text = tspan.Ticks.ToString
  11. Catch ex As Exception
  12. MessageBox.Show(ex.Message, Me.Text)
  13. End Try
  14. End Sub

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.

  1. Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_go.Click
  2. Try
  3.  
  4. 'CREATE A VARIABLE OF A TIMESPAN
  5. Dim tspan As TimeSpan
  6. 'CREATE THE VARIABLE OF A DATETIME
  7. Dim strtdate As DateTime
  8. Dim enddate As DateTime
  9.  
  10. 'SET THE CONVERTED TIME FROM THE DATETIMEPICKER
  11. 'TO THE VARIABLE THAT YOU HAVE DECLARED
  12. strtdate = DateTime.Parse(dtp_strTime.Text)
  13. enddate = DateTime.Parse(dtp_Endtime.Text)
  14. 'SET THIS FORMULA TO THE GET THE TIME INTERVAL OF TWO TIMES.
  15. tspan = enddate.Subtract(strtdate).Duration
  16.  
  17. 'PERFORM THE SUB PROCEDURE THAT YOU HAVE
  18. 'CREATED TO SEGRAGATES THE TIME INTERVAL THAT YOU HAVE GET.
  19. TimeInterval(tspan)
  20. Catch Ex As Exception
  21. MessageBox.Show(Ex.Message, Me.Text)
  22. End Try
  23. End Sub

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

  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'SET THE VALUE OF THE END DATE HIGHER THAN THE START DATE
  3. dtp_Endtime.Value = DateAdd(DateInterval.Hour, 9, dtp_Endtime.Value)
  4. dtp_Endtime.Value = DateAdd(DateInterval.Minute, 30, dtp_Endtime.Value)
  5. dtp_Endtime.Value = DateAdd(DateInterval.Second, 30, dtp_Endtime.Value)
  6. End Sub

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

 

Readers might read also:

 

 

Facebook Comments
(Visited 74 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here