gpt4 book ai didi

c# - 确定新日期是否为默认的 new DateTime()

转载 作者:太空狗 更新时间:2023-10-30 01:21:38 27 4
gpt4 key购买 nike

这是我的问题:

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 分配了正确的日期。

请指教,谢谢

最佳答案

假设您的批准日期不具有等于 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;
}

关于c# - 确定新日期是否为默认的 new DateTime(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15060618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com