本文介绍了如何在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 [ ^ ]但是,当您使用Date时,时间将始终为00:00:00(或如MSDN所描述的:具有与此实例相同日期的新对象,并且时间值设置为午夜12:00:00(00:00:00). 如果您想知道如何在用户界面中正确显示此内容,则应设置一个掩码,例如:"dd-MM-yyyy"(添加时间类似于HH:mm:ss). 或使用 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();;
在此处查看.有很多变体: www.dotnetperls/datetime-format [ ^ ] Check out here. There are many variations: www.dotnetperls/datetime-format[^]
更多推荐
如何在C#中仅获取日期值.
发布评论