Converting Days And Time in VB.Net

0
553

This tutorial is all about 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.
[vbnet] Private Sub Parse_Days_Time(ByVal TSpan As TimeSpan)
‘IN USING THE PROPERTIES OF THE TIMESPAN, IT WILL SEPERATE THE DAYS AND TIME
‘ IT DEMONSTRATES THE TimeSpan.Days, TimeSpan.Hours,
‘ TimeSpan.Minutes, TimeSpan.Seconds
‘ AND TimeSpan.Milliseconds
Try
txt_days.Text =TSpan.Days.ToString
txt_hours.Text = TSpan.Hours.ToString
txt_minute.Text = TSpan.Minutes.ToString
txt_second.Text = TSpan.Seconds.ToString
txt_millisecond.Text = TSpan.Milliseconds.ToString

Catch ex As Exception
MsgBox(ex.Message, Me.Text)
End Try
[/vbnet] In the Form1_Load, you have to set the constant value of the days and time in the TextBox.
[vbnet] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘SET THE STRING VALUE IN THE TEXTBOX
txt_daysTime.Text = “4.12:59:06.12”
End Sub
[/vbnet] 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.
[vbnet] Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Try
Dim TSpan As TimeSpan
‘CONVERTS THE STRING VALUE INTO TIMESPAN
TSpan = TimeSpan.Parse(txt_daysTime.Text)

‘SET THE VALUE OF THE TIMESPAN
‘TO THE PARAMETER OF THE SUB PROCEDURE THAT YOU HAVE CREATED.
Parse_Days_Time(TSpan)
Catch ex As Exception
MsgBox(Me.Text, ex.Message)
End Try
End Sub
[/vbnet] Download the complete source code and run it on your computer.

 

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.