The following example uses the DateTime
class to retrieve the current time. It also illustrates how a DateTime
value can be formatted using some of the standard time format strings.
This example requires a Timer Control named Timer1 and label for showing current time on a form in your Windows Form application. The Timer control allows repeat any task in a given time interval, on here we are using this control to change current time on the label control.
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Label1.Text = "Current Time is :" Timer1.Start() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick Dim CurrentDateTime As DateTime CurrentDateTime = DateTime.Now Label2.Text = CurrentDateTime.ToString("hh:mm:ss tt") End Sub
So it is easy to display the current time in vb.net. Just use the DateTime.Now
property. This will give you the current date and time. ToString
method is used to display the current date and time in desired format. We update the text of label1 on the Timer Tick event of the Timer Control show that label1 display the current time in hh:mm:ss tt
format.