这是我的问题:
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;
}
当在开头声明上一个日期时,它包含默认值DateTime()。我想要做的是找出是否已经为previousDate分配了正确的日期。
建议,谢谢
答案 0 :(得分:2)
假设您的批准日期的值不等于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;
}
<强>更新强>
根据@mdmullinax的回答,上面的代码类似于:
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;
}
答案 1 :(得分:2)
检查{/ 1}}是否
previousDate == DateTime.MinValue
相当于
DateTime previousDate = new DateTime();
来自MSDN DateTime Structure文档:
DateTime previousDate = DateTime.MinValue;