从C#中的DateTime中提取日期部分 [英] extract the date part from DateTime in C#

查看:1873
本文介绍了从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 Part。

DateTime is a DataType which is used to store both Date and Time. But it provides Properties to get the Date Part.

您可以从日期属性。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆