ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we are going to learn how to print date and time in different formats using Format() function in VB.Net?
Submitted by Nidhi, on December 06, 2020

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

Syntax:

Format(Date, StyleArg)

Parameters:

  • Date: Given the date object.
  • StyleArg: String Argument, which is used to specify the date format.

Return Value:

The Format() function will return formatted date time in the form of a string.

Program/Source Code:

The source code to print date and time in different formats using the Format() function is given below. The given program is compiled and executed successfully.

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

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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a Main() method. In the Main() method, we created two variables strDate and D. Here we assigned the current date and time into the "D" object of Date class using "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)

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

VB.Net Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.