如何在C#中获取当前日期和时间 [英] how to get the current date and Time in C#

查看:692
本文介绍了如何在C#中获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Team,



这是一个非常简单的问题,但我不知道为什么会发生这种情况。

我想要当前的日期和时间C#。

我正在尝试这个:

customerLoanRecord.CreatedOn = DateTime.Now.Date;



I我得到的日期,但我看不到时间。为时间,我该怎么办。

i需要DateandTime。



感谢

harshal raut

Hello Team,

it a very simple question but i dont know why its happening.
I want current Date and Time in C#.
I am Trying this:
customerLoanRecord.CreatedOn = DateTime.Now.Date;

I am getting the date but i cant see the time .So for Time what should i do .
i required both DateandTime.

Thank
harshal raut

推荐答案

取消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;
     }
}


这篇关于如何在C#中获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆