使用c#将Datetime转换为日期 [英] Convert Datetime into date using c#
本文介绍了使用c#将Datetime转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有'1986-03-24T00:00:00'格式的DateOfBirth,但我希望这只是'1986-03-24'
格式。
我不知道如何将日期时间转换为日期?
提前谢谢。
I have DateOfBirth in '1986-03-24T00:00:00' format but I want this as'1986-03-24'
format only.
I don't know how to convert datetime to date?
Thanks in advance.
推荐答案
赞这个:
http:// msdn。 microsoft.com/en-us/library/cc165448.aspx [ ^ ]
试试这个:
Try this:
DateTime dt = DateTime.ParseExact(yourDatetime, "yyyy/MM/DD", CultureInfo.InvariantCulture);
请参考:格式化日期时间以显示 - 格式字符串描述 [ ^ ]
如果您的初始值是DateTime
格式,那么ToString
DateTime
结构的方法可以与InvariantCulture一起使用
DateFormat
具有如下所示的所需模式:
In case your initial value is inDateTime
format then theToString
method ofDateTime
structure can be used with theInvariantCulture
DateFormat
with the required pattern as shown below:
DateTime dateVal = new DateTime(1986,03,24);
Console.WriteLine (dateVal.ToString());
Console.WriteLine (dateVal.ToString("yyyy-MM-dd",
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat));
//Output
//3/24/1986 12:00:00 AM
//1986-03-24
这篇关于使用c#将Datetime转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文