Home >>VB.Net Built-in Functions >VB.Net program to print date and time in different formats using Format() function

VB.Net program to print date and time in different formats using Format() function

Write a VB.Net program to print date and time in different formats using Format() function

Here, we used the function Format(), which is used to get the date and time formatted based on the style argument.

Syntax :

Format(Date, StyleArg)

Parameters :

  • Date: Considering the date object.
  • StyleArg: String Argument used to specify the format of the date.

Return Value :

The Format() function returns the time of the formatted date as a string.

Program :

Below is the source code for printing the date and time in various formats using the Format() function. The program given is compiled and successfully executed.


Module Module1
    Sub Main()
        Dim strDate As String
        Dim D As Date
        D = Date.Now
        strDate = Format(D, "General Date")
        Console.WriteLine(strDate)
        strDate = Format(D, "Long Date")
        Console.WriteLine(strDate)
        strDate = Format(D, "Short Date")
        Console.WriteLine(strDate)
        strDate = Format(D, "Long Time")
        Console.WriteLine(strDate)
        strDate = Format(D, "Short Time")
        Console.WriteLine(strDate)
    End Sub
End Module
	
Output:
11/24/2020 7:39:53 PM
Tuesday, November 24, 2020
11/24/2020
7:39:53 PM
7:39 PM

Explanation:

We created a Module1 module in the program above that contains the Main() method. We created two variables, strDate and D, in the Main() method. Here, using "D" we assigned the present date and time to the "Date.Now" object of the Date class.

strDate = Format(D, "General Date")
Console.WriteLine(strDate)

strDate = Format(D, "Long Date")
Console.WriteLine(strDate)

strDate = Format(D, "Short Date")
Console.WriteLine(strDate)

strDate = Format(D, "Long Time")
Console.WriteLine(strDate)

strDate = Format(D, "Short Time")
Console.WriteLine(strDate)

We used the Format() function here, passed the date object "D" and various style arguments here, and then printed the formatted date and time on the console screen.