VB.Net program to compare two dates

Here, we are going to learn how to compare two dates in VB.Net?
Submitted by Nidhi, on January 19, 2021 [Last updated : March 07, 2023]

Compare two dates in VB.Net

Here, we will compare two dates and then print appropriate according to the comparation on the console screen.

Program/Source Code:

The source code to compare the two dates is given below. The given program is compiled and executed successfully.

VB.Net code to compare two dates

'VB.NET program to compare two dates.

Imports System

Module Module1
    Sub Main()

        Dim date1 As New DateTime(2020, 3, 22)
        Dim date2 As New DateTime(2021, 5, 25)

        If (date1 < date2) Then
            Console.WriteLine("{0} is less than {1}", date1, date2)
        Else
            Console.WriteLine("{0} is less than {1}", date2, date1)
        End If
    End Sub
End Module

Output

22-03-2020 00:00:00 is less than 25-05-2021 00:00:00
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.

In the Main() function, we created two objects of the DateTime class that are initialized with date values. Then we compared both objects using less than operator and print an appropriate message on the console screen.

VB.Net Date & Time Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.