C#

How to Compare Two Dates in C#?

This program creates two DateTime objects, date1 and date2, that represent two specific dates. It then calls the Compare method of the DateTime struct to compare the two dates. The Compare method returns a negative value if the first date is earlier than the second date, a positive value if the first date is later than the second date, and 0 if the two dates are the same.

The program uses an if statement to check the value of the result variable and print a message to the console indicating which date is earlier or whether the dates are the same. The output of this program will be “5/1/2022 is earlier than 10/1/2022”.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.