C# Datetime.utc在事件查看器中返回1-1-0001 00:00:00

C# Datetime.utc在事件查看器中返回1-1-0001 00:00:00,c#,datetime,C#,Datetime,我想将当前日期返回到我的事件记录器,我尝试了以下方法: public class LogDate { protected DateTime localDate { get; set; } public LogDate() { localDate = DateTime.Now; } } 不知怎的,当我尝试这个时,它返回1-1-0001 00:00:00。而不是实际时间。您将localDate设置为新的默认值 this.localDate =

我想将当前日期返回到我的事件记录器,我尝试了以下方法:

public class LogDate
{
    protected DateTime localDate { get; set; }

    public LogDate()
    {
        localDate = DateTime.Now;

    }
}


不知怎的,当我尝试这个时,它返回1-1-0001 00:00:00。而不是实际时间。

您将
localDate
设置为新的默认值

this.localDate = new DateTime();

localDate
设置为新的默认值

this.localDate = new DateTime();

再次将
this.localDate
指定为新的
DateTime

this.localDate = new DateTime();
1-1-0001 00:00:00
new DateTime()
的默认值。要修复它,请使用
this.localDate
,而不使用
new DateTime()
重新分配它

直接使用它,因为您总是在构造函数中初始化它:

public class LogDate
{
    protected DateTime localDate { get; set; }

    public LogDate()
    {
        localDate = DateTime.Now;

    }
}

再次将
this.localDate
指定为新的
DateTime

this.localDate = new DateTime();
1-1-0001 00:00:00
new DateTime()
的默认值。要修复它,请使用
this.localDate
,而不使用
new DateTime()
重新分配它

直接使用它,因为您总是在构造函数中初始化它:

public class LogDate
{
    protected DateTime localDate { get; set; }

    public LogDate()
    {
        localDate = DateTime.Now;

    }
}

@拉斯克罗兹:好的……:)@拉斯克罗兹:好的……:)顺便说一下,代码中没有
DateTime.UtcNow
。顺便说一下,代码中没有
DateTime.UtcNow