×

VB.Net Programs

VB.Net program to demonstrate the Equals() method of DateTime class

By Nidhi Last Updated : November 11, 2024

DateTime.Equals() Method in VB.Net

The Equals() method is used to compare two date objects and returns a boolean value to specify the result of comparison.

Syntax

Function Equals(ByVal date1 as Date, ByVal date2 as Date) as Boolean

Parameter(s)

  • Date1 : First specified date object.
  • Date2 : Second specified date object.

Return Value

It returns True when two objects of the DateTime class are equal otherwise it returns False.

VB.Net code to demonstrate the example of DateTime.Equals() method

The source code to demonstrate the Equals() method of the DateTime class is given below. The given program is compiled and executed successfully.

'VB.NET program to demonstrate Equals() method of DateTime class.

Imports System

Module Module1
    Sub Main()
        Dim date1 As New DateTime(2020, 4, 27)
        Dim date2 As New DateTime(2021, 3, 28)
        Dim date3 As New DateTime(2021, 3, 28)
        Dim ret As Boolean = False

        ret = DateTime.Equals(date1, date2)
        If (ret = True) Then
            Console.WriteLine("{0} and {1} are equal", date1, date2)
        Else
            Console.WriteLine("{0} and {1} are not equal", date1, date2)
        End If

        ret = DateTime.Equals(date2, date3)
        If (ret = True) Then
            Console.WriteLine("{0} and {1} are equal", date2, date3)
        Else
            Console.WriteLine("{0} and {1} are not equal", date2, date3)
        End If
    End Sub
End Module

Output

27-04-2020 00:00:00 and 28-03-2021 00:00:00 are not equal
28-03-2021 00:00:00 and 28-03-2021 00:00:00 are equal
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 three objects of the DateTime class that are initialized with date values. Then we compared date objects using the Equals() method of the DateTime class and print the appropriate message on the console screen.

VB.Net Date & Time Programs »


Comments and Discussions!

Load comments ↻





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