如何在C#中仅获取日期值. [英] How to get only date value in C#.

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

问题描述

如何在C#中仅获取日期值.

我正在使用system.datetime.now.date
它返回带时间的日期.

任何人都可以给出解决方案

How to get only date value in C#.

i am using system.datetime.now.date
it returns date with time.

Can any one give the solution

推荐答案

没有没有时间的约会".
System.DateTime.Now [System.DateTime.Now.Date [ 或使用 DateTime.ToShortDateString [
There is no ''date without time''.
The difference between System.DateTime.Now[^] and System.DateTime.Now.Date[^] however is that when you use Date the time will always be 00:00:00 (or as MSDN describes it: A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00)).
If you''re wondering how to properly show this in your user interface you should set a mask, for example: ''dd-MM-yyyy'' (adding time would look like HH:mm:ss).
Or use DateTime.ToShortDateString[^].
Both methods require you to create a string from your date variable though.
Hope this helps :)


DateTime d = new DateTime();
String date = d.ToShortDateString()
string date = d.ToLongDateString();;


在此处查看.有很多变体:

http://www.dotnetperls.com/datetime-format [
Check out here. There are many variations:

http://www.dotnetperls.com/datetime-format[^]


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

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