ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Converting Days And Time in VB.Net

In this tutorial I will teach you how to convert the days and time in VB.Net. With this, you can segregates the combined days and time (2.06:58:08.32) into days, hours, minutes, seconds and milliseconds. This will help you and easily identify its different fields.

 

Let’s begin:

 
Open the Visual Basic, create a new Windows Form Application and do the Form just like this.
daystimeconverterform
After setting up the Form, double click the Form and create a sub procedure above the Form1_Load for separating the days and time.

  1. Private Sub Parse_Days_Time(ByVal TSpan As TimeSpan)
  2. 'IN USING THE PROPERTIES OF THE TIMESPAN, IT WILL SEPERATE THE DAYS AND TIME
  3. ' IT DEMONSTRATES THE TimeSpan.Days, TimeSpan.Hours,
  4. ' TimeSpan.Minutes, TimeSpan.Seconds
  5. ' AND TimeSpan.Milliseconds
  6. Try
  7. txt_days.Text =TSpan.Days.ToString
  8. txt_hours.Text = TSpan.Hours.ToString
  9. txt_minute.Text = TSpan.Minutes.ToString
  10. txt_second.Text = TSpan.Seconds.ToString
  11. txt_millisecond.Text = TSpan.Milliseconds.ToString
  12.  
  13. Catch ex As Exception
  14. MsgBox(ex.Message, Me.Text)
  15. End Try

In the Form1_Load, you have to set the constant 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. 'SET THE STRING VALUE IN THE TEXTBOX
  3. txt_daysTime.Text = "4.12:59:06.12"
  4. End Sub

Lastly, go to the design views, double click the “convert” Button and set the Sub Procedure that you have created in the Click event handler of the Button.

  1. Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
  2. Try
  3. Dim TSpan As TimeSpan
  4. 'CONVERTS THE STRING VALUE INTO TIMESPAN
  5. TSpan = TimeSpan.Parse(txt_daysTime.Text)
  6.  
  7. 'SET THE VALUE OF THE TIMESPAN
  8. 'TO THE PARAMETER OF THE SUB PROCEDURE THAT YOU HAVE CREATED.
  9. Parse_Days_Time(TSpan)
  10. Catch ex As Exception
  11. MsgBox(Me.Text, ex.Message)
  12. End Try
  13. End Sub

Download the complete source code and run it on your computer.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!