且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中获取当前日期和时间

更新时间:2022-11-08 23:32:12

取消Date一词:

Take off the word "Date":
customerLoanRecord.CreatedOn = DateTime.Now;



DateTime对象的Date属性删除所有时间信息!


The Date property of a DateTime object removes all time information!


Quote:

customerLoanRecord.CreatedOn = DateTime.Now.Date;

customerLoanRecord.CreatedOn = DateTime.Now.Date;

这应该是:

This should be:

customerLoanRecord.CreatedOn = DateTime.Now;





(假设 customerLoanRecord.CreatedOn 类型是日期时间


将CreatedOn声明为属性,然后检查。



Declare CreatedOn as a property and then check.

private DateTime _CreatedOn = new DateTime();
public DateTime CreatedOn
{
     get
     {
         _CreatedOn = DateTime.Now;
         return _CreatedOn;
     }
     set
     {
         _CreatedOn = value;
     }
}