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

编程入门 行业动态 更新时间:2024-10-26 10:38:44
本文介绍了如何在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#中仅获取日期值.

本文发布于:2023-11-16 16:18:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!