如何比较date在C#
我有两个date。 一个date是input,另一个是DateTime.Now 。 我有他们在mm/dd/yyyy格式,它甚至可以是m / d / yy格式也。 两个date都是可空的即数据types是DateTime?  ,因为我也可以传递null作为input。 现在我想比较这两个date只能用mm/dd/yyyy或m/d/yy格式。 
如果您在DateTimevariables中有date,则它们没有格式。
 您可以使用Date属性返回DateTime值,并将时间部分设置为午夜。 所以,如果你有: 
 DateTime dt1 = DateTime.Parse("07/12/2011"); DateTime dt2 = DateTime.Now; if(dt1.Date > dt2.Date) { //It's a later date } else { //It's an earlier or equal date } 
 如果您在DateTimevariables中有date,那么它是一个DateTime对象,并且不包含任何格式。 当您调用DateTime.ToString方法并在其中提供格式时,格式化date表示为string 。 
 比方说,你有两个DateTimevariables,你可以使用比较方法进行比较, 
 DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0); DateTime date2 = new DateTime(2009, 8, 2, 0, 0, 0); int result = DateTime.Compare(date1, date2); string relationship; if (result < 0) relationship = "is earlier than"; else if (result == 0) relationship = "is the same time as"; else relationship = "is later than"; 
源自msdn的代码片段
 首先,了解DateTime对象没有格式化。 他们只是把Year,Month,Day,Hour,Minute,Second等等作为一个数值来存储,而当你想以某种方式把它表示为一个string的时候就会发生格式化。 您可以比较DateTime对象而无需格式化它们。 
 要将inputdate与DateTime.Now进行比较,您需要先将inputparsing为date,然后再比较年/月/日部分: 
 DateTime inputDate; if(!DateTime.TryParse(inputString, out inputDate)) throw new ArgumentException("Input string not in the correct format."); if(inputDate.Date == DateTime.Now.Date) { // Same date! }