Date time format in VB.NET

Here we explained different methods to declare Date Time Format in VB.NET. This program will explain how to display date (day, month, Year, Day) and Time (Hour, Minute, Second) in VB.NET. In this program we have formatted date and time in the string pattern.


Source code:

Public Class btndatetime

    Private Sub btnd_Click(sender As Object, e As EventArgs) Handles btnd.Click
        'This will display the Date in 2 digits
        lbldatetime.Text = "Today's date is " & DateTime.Now.ToString("dd")
    End Sub

    Private Sub btnddd_Click(sender As Object, e As EventArgs) Handles btnddd.Click
        'This will display the abbrevated name of day
        lbldatetime.Text = "Today is " & DateTime.Now.ToString("ddd")
    End Sub

    Private Sub btndddd_Click(sender As Object, e As EventArgs) Handles btndddd.Click
        'This will display the full name of day
        lbldatetime.Text = "Today is " & DateTime.Now.ToString("dddd")
    End Sub

    Private Sub btnhh_Click(sender As Object, e As EventArgs) Handles btnhh.Click
        'This will display the current hour in 12 hour format
        lbldatetime.Text = "Hour (12 Hour Format): " & DateTime.Now.ToString("hh")
    End Sub

    Private Sub btn24hh_Click(sender As Object, e As EventArgs) Handles btn24hh.Click
        'This will display the current hour in 24 hour format
        lbldatetime.Text = "Hour (24 Hour Format): " & DateTime.Now.ToString("HH")
    End Sub

    Private Sub btnmm_Click(sender As Object, e As EventArgs) Handles btnmm.Click
        'This will display the minute
        lbldatetime.Text = "Minute - " & DateTime.Now.ToString("mm")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'This will display the month in digits
        lbldatetime.Text = "Month - " & DateTime.Now.ToString("MM")
    End Sub

    Private Sub btnmonth_Click(sender As Object, e As EventArgs) Handles btnmonth.Click
        'This will display the month in text
        lbldatetime.Text = "Month - " & DateTime.Now.ToString("MMMM")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' This will display the second in two digits
        lbldatetime.Text = "Second - " & DateTime.Now.ToString("ss")
    End Sub

    Private Sub btntt_Click(sender As Object, e As EventArgs) Handles btntt.Click
        'This will display the current time in AM/PM
        lbldatetime.Text = "AM / PM - " & DateTime.Now.ToString("tt")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'This will display year in two digit
        lbldatetime.Text = "Year in two digit - " & DateTime.Now.ToString("yy")
    End Sub

    Private Sub btnyyyy_Click(sender As Object, e As EventArgs) Handles btnyyyy.Click
        'This will display year in four digit
        lbldatetime.Text = "Year in four digit - " & DateTime.Now.ToString("yyyy")
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        'This will display Date, month, Year , Hour, Minute, Second and AM/PM
        lbldatetime.Text = DateTime.Now.ToString("dd - MM - yyyy hh:mm:ss tt")
    End Sub

End Class

Video Tutorial:


Download Example Code:

download

🔥301 Views

Anu

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.