Monday, 21 January 2013

How to change The Timer time format in vb.net

In this article I am going to tell how to change the timer time format using string format. Conceptually this is simple. in practice there are some details you must account for if you want a functional program. Formatting your DateTime as a string is straightforward with an understanding of the syntax.

As we begin, please notice how the current DateTime is acquired through the DateTime.Now property. When you execute these code examples, the current DateTime will be different.
 
Exmaple:

12 Hours Format:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System. 
EventArgs)  
Handles MyBase.Load
        Timer1 = New Timer
        Timer1.Interval = 1000

        Timer1.Start()
        
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As  
System.EventArgs
Handles Timer1.Tick
        Label1.Text = TimeOfDay

    End Sub

   
End Class

24 Hours format:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
 System.EventArgs)  
Handles MyBase.Load
        
        Timer2 = New Timer
        Timer2.Interval = 1000
        Timer2.Start()
    End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As  
System.EventArgs)  
Handles Timer2.Tick
        Dim time As DateTime = DateTime.Now
        Dim format As String = "HH:mm:ss" ‘ this is using to define the string
format.
        Label2.Text = time.ToString(format)
    End Sub
End Class


No comments:

Post a Comment