How to Get a Time Interval in VB.NET

Today, I’m going to teach you how to get a time interval in VB.NET. In here, I use the TimeSpan properties for getting the time interval. It segregates the hours, minutes, seconds, millisecond and even the tick of the clock. Then it will appear in each corresponding text boxes. I used Microsoft Visual Studio for creating this application. Let’s begin: Open Microsoft Visual Studio, create a new Project then name it “TimeInterval” and do the Form just like this. First Form Go to the solution explorer and click the view code. View Code In the code view, create a sub procedure for separating the time intervals of the corresponding fields.
  1.   Private Sub TimeInterval(ByVal ts As TimeSpan)
  2.  
  3.         'USE THE PROPERTIES OF THE TIMESPAN AND IT DEMONSTRATE  TimeSpan.Hours
  4.         ',  TimeSpan.Milliseconds ,  TimeSpan.Minutes  , TimeSpan.Seconds
  5.         ' AND TimeSpan.Ticks
  6.         Try
  7.  
  8.             txthours.Text = ts.Hours.ToString
  9.             txtmillisecond.Text = ts.Milliseconds.ToString
  10.             txtminutes.Text = ts.Minutes.ToString
  11.             txtseconds.Text = ts.Seconds.ToString
  12.             txtticks.Text = ts.Ticks.ToString
  13.         Catch ex As Exception
  14.             MessageBox.Show(ex.Message, Me.Text)
  15.         End Try
  16.     End Sub
After that, do this following code in the click event of the Button to get the time interval.
  1.    Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngo.Click
  2.         Try
  3.          
  4.             'CREATE A VARIABLE OF A TIMESPAN
  5.             Dim time_span As TimeSpan
  6.             'CREATE THE VARIABLE OF A DATETIME
  7.             Dim strt_Date As DateTime
  8.             Dim end_Date As DateTime
  9.  
  10.  
  11.             'SET AND CONVERT THE TIME FROM THE DATETIMEPICKER
  12.             strt_Date = DateTime.Parse(dtpstrTime.Text)
  13.             end_Date = DateTime.Parse(dtpEndtime.Text)
  14.             'SUBTRACT THE STARTING TIME AND THE END TIME
  15.             time_span = end_Date.Subtract(strt_Date).Duration
  16.  
  17.            
  18.             'PERFORM THE SUB PROCEDURE THAT YOU HAVE
  19.             'CREATED IN DISPLAYING THE TIME INTERVAL
  20.             TimeInterval(time_span)
  21.         Catch Ex As Exception
  22.             MessageBox.Show(Ex.Message, Me.Text)
  23.         End Try
  24.     End Sub
Then, do this following code on the first load. It sets the value of the end time higher than the starting time.
  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.         dtpEndtime.Value = DateAdd(DateInterval.Hour, 9, dtpEndtime.Value)
  4.         dtpEndtime.Value = DateAdd(DateInterval.Minute, 30, dtpEndtime.Value)
  5.         dtpEndtime.Value = DateAdd(DateInterval.Second, 30, dtpEndtime.Value)
  6.  
  7.     End Sub
Finally, press F5 to run your project. Output: Output

Add new comment