Determining whether a new date is a new default DateTime () or not

Here is my question:

DateTime previousDate = new DateTime(); DateTime currentDate = new DateTime(); foreach (ApproverVo approver in approvers) { if (previousDate != null) { currentDate = (DateTime)approver.ApprovalDate; totalTimeSpan += (currentDate - previousDate).TotalDays; previousDate = currentDate; } else previousDate = (DateTime)approver.ApprovalDate; } 

When the previous date is declared at the beginning, it contains the default DateTime (). What I want to do is find out if the previous date is assigned to the corresponding date or not.

Advice please thank you

+4
source share
2 answers

Suppose the date of your statement does not matter DateTime.MinValue :

 DateTime previousDate = DateTime.MinValue; DateTime currentDate = new DateTime(); foreach (ApproverVo approver in approvers) { if (previousDate != DateTime.MinValue) { currentDate = (DateTime)approver.ApprovalDate; totalTimeSpan += (currentDate - previousDate).TotalDays; previousDate = currentDate; } else previousDate = (DateTime)approver.ApprovalDate; } 

UPDATE

According to @mdmullinax answers, the code above is similar:

 DateTime previousDate = new DateTime(); DateTime currentDate = new DateTime(); foreach (ApproverVo approver in approvers) { if (previousDate != new DateTime()) { currentDate = (DateTime)approver.ApprovalDate; totalTimeSpan += (currentDate - previousDate).TotalDays; previousDate = currentDate; } else previousDate = (DateTime)approver.ApprovalDate; } 
+2
source

check if previousDate == DateTime.MinValue since

 DateTime previousDate = new DateTime(); 

equivalently

 DateTime previousDate = DateTime.MinValue; 

from the MSDN DateTime Structure Documentation :

 DateTime dat1 = new DateTime(); // The following method call displays 1/1/0001 12:00:00 AM. Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture)); // The following method call displays True. Console.WriteLine(dat1.Equals(DateTime.MinValue)); 
+2
source

All Articles