The below code works fine :
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);
int d3 = (int)(d1 - d2).TotalDays;
But what if I define DateTime
as DateTime?
:
DateTime? d1 = DateTime.Now;
DateTime? d2 = DateTime.Now.AddDays(-1);
int d3 = (int)(d1 - d2).TotalDays;
underlined red with error
Cannot implicitly convert 'System.TimeSpan?' to 'System.TimeSpan'
Is it possible to get the difference of number of days between two datetimes that are defined as nullable?
int d3 = (int)(d1.Value - d2.Value).TotalDays;
– Anthea