How to compare only Date without Time in DateTime types in C#.One of the date will be nullable.How can i do that??
-
1I don't understand your question. Can you please clarify it? Just use `DateTime? < DateTime` ? – Soner Gönül Oct 16 '14 at 12:21
-
i want to compare two datetime object without conisdering time..one of date object is nullable type – vmb Oct 16 '14 at 12:22
-
Do you need to handle different timezones, daylight savings time transitions, etc.? – Lasse V. Karlsen Oct 16 '14 at 12:30
7 Answers
DateTime val1;
DateTime? val2;
if (!val2.HasValue)
return false;
return val1.Date == val2.Value.Date;

- 1,997
- 14
- 15
-
...+1. Or trenary operator: `return val2.HasValue ? val1.Date == val2.Value.Date : false;` – Dmitry Bychenko Oct 16 '14 at 12:34
-
@DmitryBychenko - The conditional operator seems a bit cumbersome here. How about just `return val2.HasValue && val1.Date == val2.Value.Date;` ? – Chris Dunaway Oct 16 '14 at 14:04
You can use the Date
property of DateTime
object
Datetime x;
Datetime? y;
if (y != null && y.HasValue && x.Date == y.Value.Date)
{
//DoSomething
}

- 2,808
- 14
- 25
This uses short-circuit to avoid comparisons if nullable date is null, then uses the DateTime.Date property to determine equivalency.
bool Comparison(DateTime? nullableDate, DateTime aDate) {
if(nullableDate != null && aDate.Date == nullableDate.Value.Date) {
return true;
}
return false;
}

- 5,003
- 4
- 33
- 62
bool DatesMatch(DateTime referenceDate, DateTime? nullableDate)
{
return (nullableDate.HasValue) ?
referenceDate.Date == nullableDate.Value.Date :
false;
}

- 6,598
- 2
- 26
- 42
If you want a true comparison, you can use:
Datetime dateTime1
Datetime? dateTime2
if(dateTime2.Date != null)
dateTime1.Date.CompareTo(dateTime2.Date);
Hope it helps...

- 1,532
- 1
- 15
- 31
The only challenging aspect here is the fact that you want something which is both DateTime and nullable.
Here is the solution for standard DateTime: How to compare only Date without Time in DateTime types in C#?
if(dtOne.Date == dtTwo.Date)
For nullable, it's just a matter of choice. I would choose an extension method.
class Program
{
static void Main(string[] args)
{
var d1 = new DateTime(2000, 01, 01, 12, 24, 48);
DateTime? d2 = new DateTime(2000, 01, 01, 07, 29, 31);
Console.WriteLine((d1.Date == ((DateTime)d2).Date));
Console.WriteLine((d1.CompareDate(d2)));
Console.WriteLine((d1.CompareDate(null)));
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}
static class DateCompare
{
public static bool CompareDate(this DateTime dtOne, DateTime? dtTwo)
{
if (dtTwo == null) return false;
return (dtOne.Date == ((DateTime)dtTwo).Date);
}
}
-
Instead of casting dtTwo you can get the date through dtTwo.Value.Date – Marcobdv Oct 16 '14 at 12:53
You could create a method like the one below, following the similar return values as the .net framework comparisons, giving -1 when the left side is smallest, 0 for equal dates, and +1 for right side smallest:
private static int Compare(DateTime? firstDate, DateTime? secondDate)
{
if(!firstDate.HasValue && !secondDate.HasValue)
return 0;
if (!firstDate.HasValue)
return -1;
if (!secondDate.HasValue)
return 1;
else
return DateTime.Compare(firstDate.Value.Date, secondDate.Value.Date);
}
Of Course a nicer implementation would be to create an extension method for this.

- 87
- 3