In this article, you will learn how to compare two dates without time in C#. Sometimes, we need to compare only the date parts of two DateTime variables in C#. So here in this article, we used the == operator and .CompareTo() method to compare the two dates without time in C#. Show Here are the examples to compare two dates without time in C#. Example 1: Using == OperatorIn this example, we compare the two dates without time using the equality == operator, if both dates are the same then it will return true otherwise it will return false. Here is the source code of the program to compare two dates without time using == Operator in C#.
using System; namespace Tutorialsrack { class Program { /* How to compare two Dates without time in C# */ static void Main(string[] args) { DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(-50); if (date1.Date == date2.Date) { Console.WriteLine("Both the dates are same"); } else { Console.WriteLine("Both the dates are not same"); } //Hit ENTER to exit the program Console.ReadKey(); } } } Example 2: Using CompareTo() MethodIn this example, we compare the value of this instance to a specified DateTime value and indicate whether this instance is earlier than, the same as, or later than the specified DateTime value. A number indicating the relative values of this instance and the value parameter. Compare Return Value:
Here is the source code of the program to compare the two dates without time using the .CompareTo() method in c#.
using System; namespace Tutorialsrack { class Program { /* How to compare two Dates without time in C# */ static void Main(string[] args) { DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(-50); var compare = date1.Date.CompareTo(date2.Date); switch (compare) { case 1: Console.WriteLine("The Date1 is greater than the Date2."); break; case 0: Console.WriteLine("The Date1 is the same as the Date2."); break; default: Console.WriteLine("The Date1 is earlier date than the Date2."); break; } //Hit ENTER to exit the program Console.ReadKey(); } } } I hope this article will help you to understand how to compare two dates without time in C#. Share your valuable feedback, please post your comment at the bottom of this article. Thank you! Here you will learn which is the best way to check whether the two strings are equal or not in C#. You can check the equality of strings using two ways:
C# also includes String.Compare() and String.CompareTo() method, but these methods are not meant to compare string equality but rather meant to check the relative positions of strings in sorted order. Here, we are only interested in checking the equality of two string and not the position in sorting order, so we will not cover it. Let's see different scenarios of comparing string equalities. Compare Case-Sensitive StringsBoth, == and Equals() method compares the content of strings. So, there is no difference between them when you compare strings case-sensitive and in en culture.
string str1 = "London"; string str2 = "London"; str1 == str2; // true str1.Equals(str2); // true What happens if a string is null?
string str1 = "London"; string str2 = null; str1 == str2; // false str1.Equals(str2); // false str2.Equals(str1); // NullReferenceException As you can see above, there is no problem with == operator if a string is null. But, calling the Equals() method on null will throw the NullReferenceException. So, you have to make sure a string is not null before calling the Equals() method. Now, consider the following example where comparing a string with an object type.
string str1 = "London"; object obj = "London"; str1 == obj; // true str1.Equals( obj); // true obj.Equals(str1); // true So, it gives the correct result when comparing a string with an object. Now, let's see a little complicated scenario.
string str = "London"; StringBuilder sb = new StringBuilder("London"); object obj = sb.ToString(); str == obj; // false str == sb.ToString();// true str.Equals(obj);// true obj.Equals(str1); //true In the above example, str == obj returns false even though the values are the same. Why? The String type implements == operator overload, which compares the value of two operands. However, after casting StringBuilder to object, it calls different overloads where it compares reference of the two operands. So, str == obj gives the wrong result. So, if you are comparing strings case-sensitive, then in most cases == and Equals() will behave the same. However, in the scenario like above, == gives the wrong result. Compare Case-Insensitive StringsThe == operator always compares strings case-sensitive.
string str1 = "LONDON"; string str2 = "london"; str1 == str2; //false Use the Equals() method to compare strings case-insensitive using StringComparison parameter.
string str1 = "LONDON"; string str2 = "london"; str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true Always make sure that string is not null using null-conditional operator ? before calling Equals() method, as shown below.
string str1 = null; string str2 = "london"; str1?.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true == vs Equals
ConclusionIf you are sure that the type of two operands are string and want to compare string case-sensitive, then both will give the right result. However, you don't know the type of operands and want to compare strings case-insensitive or want to compare culture-specific strings then use the Equals() method. Just make sure that a string is not null on which you call the Equals() method. Learn about string comparison in detail.
CsharpServer Side ProgrammingProgramming The DateTime.Compare() method in C# is used for comparison of two DateTime instances. It returns an integer value,
SyntaxFollowing is the syntax − public static int Compare (DateTime d1, DateTime d2);Above, d1 and d2 are the two dates to be compared. ExampleLet us now see an example to implement the DateTime.Compare() method − using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40); DateTime d2 = d1.AddYears(5); Console.WriteLine("Initial DateTime = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); Console.WriteLine("New DateTime (adding years) = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); int res = DateTime.Compare(d1, d2); // returns <0 since d1 is earlier than d2 Console.WriteLine(res); } }OutputThis will produce the following output − Initial DateTime = 20 November 2019, 06:20:40 New DateTime (adding years) = 20 November 2024, 06:20:40 -1ExampleLet us now see another example to implement the DateTime.Compare() method − using System; public class Demo { public static void Main(){ DateTime d1 = new DateTime(2019, 11, 20, 6, 20, 40); DateTime d2 = new DateTime(2019, 11, 20, 6, 20, 40); Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d1); Console.WriteLine("DateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", d2); int res = DateTime.Compare(d1, d2); // returns equal to 0 since d1 is equal to d2 Console.WriteLine(res); } }OutputThis will produce the following output − DateTime 1 = 20 November 2019, 06:20:40 DateTime 2 = 20 November 2019, 06:20:40 0
Published on 11-Nov-2019 05:53:50 |