How to Convert the Days And Time in Visual Basic 2008

In this tutorial I will teach you how to convert the combined days and time by using Visual Basic 2008. This kind of method segregates the combined days and time (2.06:58:08.32) into days, hours, minutes, seconds and milliseconds. So that you can easily identify its different fields. Let’s begin: Open Visual Basic 2008, create a new Windows Application and do the Form just like this. Main form After that, double click the Form and create a sub procedure above the Form1_Load for separating the days and time.
  1. Private Sub ParseDaysTime(ByVal time_span As TimeSpan)
  2.  
  3. 'USE THE PROPERTIES OF THE TIMESPAN TO SEPERATE THE DAYS AND TIME
  4. ' IT DEMONSTRATES THE TimeSpan.Days, TimeSpan.Hours,
  5. ' TimeSpan.Minutes, TimeSpan.Seconds
  6. ' AND TimeSpan.Milliseconds
  7. Try
  8. txtdays.Text = time_span.Days.ToString
  9. txthours.Text = time_span.Hours.ToString
  10. txtminute.Text = time_span.Minutes.ToString
  11. txtsecond.Text = time_span.Seconds.ToString
  12. txtmillisecond.Text = time_span.Milliseconds.ToString
  13.  
  14. Catch ex As Exception
  15. MsgBox(ex.Message, Me.Text)
  16. End Try
In the Form1_Load, you have to set the value of the days and time in the TextBox.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'SETS THE STRING VALUE IN THE TEXTBOX
  3. txtdaysTime.Text = "5.12:59:06.12"
  4. End Sub
Lastly, go to the design views, double click the “convert” Button and do the following code in the btnConvert_Click.
  1. Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
  2.  
  3. Try
  4. Dim time_span As TimeSpan
  5. 'CONVERTS THE STRING VALUE INTO TIMESPAN
  6. time_span = TimeSpan.Parse(txtdaysTime.Text)
  7.  
  8. 'PUT THE VALUE OF THE TIMESPAN
  9. 'TO THE PARAMETER OF THE SUB PROCEDURE THAT YOU HAVE CREATED.
  10. Parse_TimeSpan(time_span)
  11. Catch ex As Exception
  12. MsgBox(Me.Text, ex.Message)
  13. End Try
  14.  
  15. End Sub
Download the complete sourcecode and run it on your computer.

Add new comment