如何仅在DateTime对象中删除C#中日期的时间部分? [英] How to remove time portion of date in C# in DateTime object only?
本文介绍了如何仅在DateTime对象中删除C#中日期的时间部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要删除日期时间的时间部分,或者可能以object
形式而不是string
形式使用以下格式的日期.
I need to remove time portion of date time or probably have the date in following format in object
form not in the form of string
.
06/26/2009 00:00:00:000
我不能使用任何 string
转换方法,因为我需要 object
形式的日期.
I can not use any string
conversion methods as I need the date in object
form.
我首先尝试将 DateTime
转换为 string
,从中删除特定时间的日期,但它添加了 12:00:00 AM
一旦我将其转换回 DateTime
object
再次返回.
I tried first converting the DateTime
to a string
, remove the time specific date from it, but it adds 12:00:00 AM
as soon as I convert it back to DateTime
object
back again.
推荐答案
您可以使用格式字符串为输出字符串指定您喜欢的格式.
You can use format strings to give the output string the format you like.
DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
阅读有关 自定义日期和时间格式字符串.
这篇关于如何仅在DateTime对象中删除C#中日期的时间部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文