I have two DateTime variables of Nullable types.
DateTime? date1 = new DateTime();
DateTime? date2 = new DateTime();
I need to get the differe
I would write something like:
int? daysBetweenDate = date1.HasValue && date2.HasValue ? (int?)(date2.Value - date2.Value).TotalDays : null;
So daysBetweenDate
is null
if at least one of date1
and date2
is null
.
If your goal is to return null when one (or both) of the dates is null, you can write like this (if you can use ?
operator which is available in C# 6.0 compiler):
int? days = (int?) (date1 - date2)?.TotalDays;