![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DateTime.Equals() Method in C#
The DateTime.Equals() method in C# is used check whether two DateTime objects or instances are equal or not. TRUE is returned if both are equal, else FALSE would be the return value.
Syntax
Following is the syntax −
public static bool Equals (DateTime date1, DateTime date2);
Example
Let us now see an example to implement the DateTime.Equals() method −
using System; public class Demo { public static void Main() { DateTime d1 = new DateTime(2019, 09, 10, 5, 15, 25); DateTime d2 = d1.AddMonths(25); Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); Console.WriteLine("
New DateTime (After adding months) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); bool res = DateTime.Equals(d1, d2); if (res) Console.WriteLine("
date1 = date2. "); else Console.WriteLine("
date1 is not equal to date2. "); } }
Output
This will produce the following output −
Initial DateTime = 10 September 2019, 05:15:25 New DateTime (After adding months) = 10 October 2021, 05:15:25 date1 is not equal to date2.
Example
Let us now see another example to implement the DateTime.Equals() method −
using System; public class Demo { public static void Main() { DateTime d1 = new DateTime(2019, 11, 11, 7, 15, 30); DateTime d2 = new DateTime(2019, 11, 11, 7, 15, 30); Console.WriteLine("DateTime1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); Console.WriteLine("
DateTime2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); bool res = DateTime.Equals(d1, d2); if (res) Console.WriteLine("
date1 = date2 "); else Console.WriteLine("
date1 is not equal to date2 "); } }
Output
This will produce the following output −
DateTime1 = 10 September 2019, 05:15:25 DateTime2 = 10 September 2019, 05:15:25 date1 = date2
Advertisements