C# 从 DateTime 中提取日期部分

发布于 2024-12-09 13:54:04 字数 126 浏览 2 评论 0原文

代码行 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぃ双果 2024-12-16 13:54:04

DateTime 是一种数据类型,用于存储DateTime。但它提供了属性来获取Date部分。

您可以从 Date 属性获取日期部分。

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

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.

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
长安忆 2024-12-16 13:54:04

没有办法“丢弃”时间部分。

DateTime.Today 与以下内容相同:

DateTime d = DateTime.Now.Date;

如果您只想显示日期部分,只需执行此操作 - 将 ToString 与格式字符串一起使用你需要。

例如,使用标准格式字符串“D”(长日期格式说明符):

d.ToString("D");

There is no way to "discard" the time component.

DateTime.Today is the same as:

DateTime d = DateTime.Now.Date;

If you only want to display only the date portion, simply do that - use ToString with the format string you need.

For example, using the standard format string "D" (long date format specifier):

d.ToString("D");
玉环 2024-12-16 13:54:04

仅比较数据时间的日期时,请使用 Date 属性。所以这应该适合你

datetime1.Date == datetime2.Date

When comparing only the date of the datatimes, use the Date property. So this should work fine for you

datetime1.Date == datetime2.Date
べ映画 2024-12-16 13:54:04
DateTime d = DateTime.Today.Date;
Console.WriteLine(d.ToShortDateString()); // outputs just date

如果您想比较日期,忽略时间部分,请使用 DateTime.YearDateTime.DayOfYear 属性。

代码片段

DateTime d1 = DateTime.Today;
DateTime d2 = DateTime.Today.AddDays(3);
if (d1.Year < d2.Year)
    Console.WriteLine("d1 < d2");
else
    if (d1.DayOfYear < d2.DayOfYear)
        Console.WriteLine("d1 < d2");
DateTime d = DateTime.Today.Date;
Console.WriteLine(d.ToShortDateString()); // outputs just date

if you want to compare dates, ignoring the time part, make an use of DateTime.Year and DateTime.DayOfYear properties.

code snippet

DateTime d1 = DateTime.Today;
DateTime d2 = DateTime.Today.AddDays(3);
if (d1.Year < d2.Year)
    Console.WriteLine("d1 < d2");
else
    if (d1.DayOfYear < d2.DayOfYear)
        Console.WriteLine("d1 < d2");
终难遇 2024-12-16 13:54:04

你可以使用格式字符串

DateTime time = DateTime.Now;              
String format = "MMM ddd d HH:mm yyyy";     
Console.WriteLine(time.ToString(format));

you can use a formatstring

DateTime time = DateTime.Now;              
String format = "MMM ddd d HH:mm yyyy";     
Console.WriteLine(time.ToString(format));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文