解压在C#中的日期部分从日期时间 [英] extract the date part from DateTime in C#
本文介绍了解压在C#中的日期部分从日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
code 的DateTime D = DateTime.Today的线;
的结果 10/12/2011上午12点00分00秒
。我怎样才能得到只有日期part.I需要我的时候比较两个日期忽略时间的一部分。
The line of code DateTime d = DateTime.Today;
results in 10/12/2011 12:00:00 AM
. How can I get only the date part.I need to ignore the time part when I compare two dates.
推荐答案
的DateTime
是被用来存储日期一个数据类型
和时间
。但它提供属性以获得日期
部分。
DateTime
is a DataType which is used to store both Date
and Time
. But it provides Properties to get the Date
Part.
您可以得到部分的日期从日期
属性。
You can get the Date part from Date
Property.
<一个href=\"http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx\">http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
// The example displays the following output to the console:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
这篇关于解压在C#中的日期部分从日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文