将Datetime转换为Date并存储在DateTime变量中 [英] Convert Datetime to Date and store in DateTime variable

查看:104
本文介绍了将Datetime转换为Date并存储在DateTime变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello EveryOne,



我脑子里有一个非常愚蠢的问题,



我有声明一个DateTime变量,如下所示



DateTime curDate = DateTime.Now;



所以这将返回输出像21-07-2014 03:45:45



但我想删除上述日期的时间戳,也想将它存储在变量curDate中我提到上面



for ex: -



DateTime CurDate = 21-07-2014





注意: - 我知道将dateTime转换为Date并存储在字符串中但我想存储在Datetime变量中的方法



这可能吗?



提前感谢

Hello EveryOne,

I have a very silly question in my mind,

I have declared a DateTime Variable like as below

DateTime curDate=DateTime.Now;

so this returns the output like 21-07-2014 03:45:45

but i want to remove the timestamp of the above date and also want to store it in variable curDate which i mention above

for ex:-

DateTime CurDate=21-07-2014


Note:- I know the method of converting dateTime to Date and store in string but i want to store in Datetime variable

is this possible?

thanks in advance

推荐答案

听起来有两件事情在这里混淆。



首先,要获得今天的日期,请使用 DateTime.Today 而不是 DateTime.Now 。或者,你可以使用 DateTime.Now.Date



这仍然是一个DateTime,因此有一段时间组件,但它总是午夜。



如果你想在没有时间组件的情况下显示它,那么在你的字符串格式中指定它。
Sounds like there are two things being mixed up here.

Firstly, to get today's date use DateTime.Today rather than DateTime.Now. Alternatively, you can use DateTime.Now.Date.

This is still a DateTime and hence has a time component, but it is always midnight.

If you want to display this without the time component, then specify that in your string formatting.


DateTime对象总是有一个Time组件,如果你不想显示它,你可以这样做:

curDate.Date

只返回日期组件。



我总是建议保存时间组件,然后只显示当你想在屏幕上显示它时需要的部分。
The "DateTime" object always has a "Time" component, if you don't want to display it you can just do:
curDate.Date
which returns only the date component.

I would always suggest saving with the time component, and then just displaying the parts you need when you want to show it on screen.


你好



对DateTime类使用ToShortDateString()方法。



Hi

Use ToShortDateString() method for DateTime class.

DateTime.Now.ToShortDateString().ToString()


这篇关于将Datetime转换为Date并存储在DateTime变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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