简体   繁体   English

DateTime.ParseExact返回当前日期

[英]DateTime.ParseExact returns current date

I tried the following code: 我尝试了以下代码:

DateTime dateTime = DateTime.ParseExact("01/02/2013",  //string date
                                        "01/02/2013",  // string format
                                         CultureInfo.InvariantCulture);

I know the format is incorrect. 我知道格式不正确。 But why instead of throwing exception it returned the current date dateTime = {24/09/2014 12:00:00 AM} . 但是为什么不是抛出异常而是返回当前日期dateTime = {24/09/2014 12:00:00 AM}

I know the valid format for my date should be MM/dd/yyyy , But Why it didn't throw an exception. 我知道我的约会的有效格式应该是MM/dd/yyyy ,但为什么它没有抛出异常。 I also tried it with DateTime.TryParseExact , it returns the current date instead of default(DateTime) . 我也尝试使用DateTime.TryParseExact ,它返回当前日期而不是default(DateTime) This actually came up reading this question . 这实际上是在阅读这个问题

My question is how does this parsing work ? 我的问题是这个解析是如何工作的?

As per MSDN : 根据MSDN

If format defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00). 如果format定义了一个没有time元素的日期并且解析操作成功,则生成的DateTime值的时间为午夜(00:00:00)。 If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date. 如果format定义没有日期元素的时间并且解析操作成功,则生成的DateTime值的日期为DateTime.Now.Date。

Your format string is the same as value - so parsing goes 'as is' and no exception thrown. 您的格式字符串与值相同 - 因此解析将“按原样”进行,并且不会抛出任何异常。 If you will change format string to say, 02/02/2013 - you'll get FormatException as expected 如果您将格式字符串更改为02/02/2013 - 您将按预期获得FormatException

There is no day/month placeholders in format string. 格式字符串中没有日/月占位符。 So it literally matches each character (successfully) and returns default (today) values for each component of the date. 因此它按字面匹配每个字符(成功)并返回日期的每个组件的默认(今天)值。

Indeed if there is no exact match it will throw error like (notice mismatch between "11/..." and "01/...") 事实上,如果没有完全匹配,它将抛出错误(注意“11 / ...”和“01 / ...”之间不匹配)

   DateTime.ParseExact("11/02/2013",  
                  "01/02/2013",  // string format
                   CultureInfo.InvariantCulture);

Behavior is very similar to some reasonable patterns like "MM/yyyy" - expecting month, than exact match to / character , than year. 行为非常类似于某些合理的模式,如“MM / yyyy” - 期望月份,而不是完全匹配/字符 ,而不是年份。

Default values are midnight of current date DateTime.ParseExact : 默认值是当前日期的午夜 DateTime.ParseExact

If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date. 如果format定义没有日期元素的时间并且解析操作成功,则生成的DateTime值的日期为DateTime.Now.Date。

From MSDN : 来自MSDN

public static DateTime ParseExact(string s, string format, IFormatProvider provider)

If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date. 如果format定义没有日期元素的时间并且解析操作成功,则生成的DateTime值的日期为DateTime.Now.Date。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM