C# 如何比较日期时间?在getter中记录时间
我在比较类中的属性时遇到问题。我有一个名为IsActive和DateTime的属性?排放。现在,我们将默认值的值从null切换到9999-12-12 00:00:00,即使对于DischargeDate=9999-12-12 00:00:00,也返回falseC# 如何比较日期时间?在getter中记录时间,c#,datetime,C#,Datetime,我在比较类中的属性时遇到问题。我有一个名为IsActive和DateTime的属性?排放。现在,我们将默认值的值从null切换到9999-12-12 00:00:00,即使对于DischargeDate=9999-12-12 00:00:00,也返回false public DateTime? DischargeDate { get; set; } public bool IsActive { get {
public DateTime? DischargeDate { get; set; }
public bool IsActive
{
get
{
return this.DischargeDate.Equals("12/12/9999 12:00:00 AM");
//return !this.DischargeDate.HasValue;
}
}
首先,您必须检查
DateTime?
是否实际有值或null
,然后您可以构造第二个DateTime
来匹配它们:
return this.DischargeDate != null
&& this.DischargeDate.Value == new DateTime(9999, 12, 12, 12, 0, 0);
首先,您必须检查
DateTime?
是否实际有值或null
,然后您可以构造第二个DateTime
来匹配它们:
return this.DischargeDate != null
&& this.DischargeDate.Value == new DateTime(9999, 12, 12, 12, 0, 0);