从 C# 中的 DateTime 中提取日期部分 [英] extract the date part from DateTime in C#
本文介绍了从 C# 中的 DateTime 中提取日期部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码行 DateTime d = DateTime.Today;
导致 10/12/2011 12:00:00 AM
.我怎样才能只得到日期部分.当我比较两个日期时,我需要忽略时间部分.
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
是一种 DataType,用于存储 Date
和 Time
>.但是它提供了属性来获取Date
部分.
DateTime
is a DataType which is used to store both Date
and Time
. But it provides Properties to get the Date
Part.
您可以从 Date
属性中获取日期部分.
You can get the Date part from Date
Property.
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# 中的 DateTime 中提取日期部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文