简体   繁体   English

DateTime.ParseExact失败取决于解析的字符串的长度

[英]DateTime.ParseExact fails depending on the parsed string's length

This is very weird. 这很奇怪。 I have the following code: 我有以下代码:

Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact("2000", "yyyy", CultureInfo.InvariantCulture));
Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact("20000705", "yyyy", CultureInfo.InvariantCulture));

The first line passes, the second one fails with " System.FormatException: String was not recognized as a valid DateTime ". 第一行通过,第二行失败,显示“ System.FormatException: String was not recognized as a valid DateTime ”。

I cannot shorten the string to be parsed to match the length of the format - it would work in this particular case, but this is part of a more generic method, and it would fail somewhere else. 我无法缩短要解析的字符串以匹配格式的长度-在这种情况下可以使用,但这是更通用的方法的一部分,在其他地方会失败。 Any idea on why the second call fails? 关于第二个呼叫为何失败的任何想法?

[Edit] Ok, I was hoping it was parsing just as much of the input string as needed to satisfy the format. [编辑]好的,我希望它可以解析满足格式所需的输入字符串。

I get a lot of strings in a lot of formats, and I have one method that accepts both and, after a bit of processing (I only get dates, so I replace "m" with "M" and "Y" with "y" and so on), I call DateTime.ParseExact . 我得到了许多格式很多的字符串,并且我有一个方法可以接受两种格式,并且经过一些处理(我只得到日期,所以我将“ m”替换为“ M”,将“ Y”替换为“ y” (依此类推),我称为DateTime.ParseExact The reason I cannot call DateTime.Parse is because it doesn't allow for a format argument... I can get ddMMyyyy in one part of the program and yyyyddMM in another, there's no way for it to correctly figure it out. 我之所以不能调用DateTime.Parse是因为它不允许使用格式参数。我可以在程序的一个部分中获取ddMMyyyy,而在另一部分中获取yyyyddMM,因此无法正确地找到它。

[Edit 2] I guess it's my fault... I'll have to truncate the input when that issue arises. [编辑2]我猜这是我的错...出现此问题时,我必须截断输入。 Fortunately I generally have all parts of the date in the format, this (just the year) is a rare occurence. 幸运的是,我通常以格式显示日期的所有部分,这种情况(仅是年份)很少见。 Thanks for the help. 谢谢您的帮助。

It's absolutely right to fail - you've told it to expect exactly 4 characters, and then given it 8! 失败绝对是正确的-您已经告诉它要精确到4个字符,然后再给它8个字符!

Options: 选项:

  • Provide more format strings (eg "yyyyMMdd") to the call (you can specify multiple valid formats) 为呼叫提供更多格式字符串(例如“ yyyyMMdd”)(您可以指定多种有效格式)
  • Only pass in the piece of the string you expect to match the format 仅传递您希望与格式匹配的字符串部分
  • Parse the string yourself 自己解析字符串

You claim that truncating the string "would fail somewhere else" - could you give more details? 您声称将字符串截断“将在其他地方失败”-您能提供更多详细信息吗? If you're specifying "yyyy" then presumably you know you need exactly 4 characters... do you know exactly where they'll be within the string? 如果您指定“ yyyy”,那么大概您知道需要4个字符...您确切知道它们在字符串中的位置吗? If so, why can't you perform the truncation? 如果是这样,为什么您不能执行截断?

It failed because DateTime.ParseExact requires the input string to match the specified format exactly (err, I mean almost exactly , per your comment below), and "20000705" does not match "yyyy". 之所以失败,是因为DateTime.ParseExact要求输入字符串与指定的格式完全匹配(错误,我的意思是, 几乎完全按照下面的注释),并且“ 20000705”与“ yyyy”不匹配。

To parse "20000705", you'll need to use a different technique - either ParseExact with an appropriate format string (like "yyyyMMdd"), or DateTime.Parse , which is a bit more forgiving (though also less accurate). 要解析“ 20000705”,您将需要使用另一种技术-具有适当格式字符串的ParseExact (例如“ yyyyMMdd”)或DateTime.Parse ,这会更加宽容(尽管准确性也较低)。

It looks like you're trying to extract the year from the date string - no parse method will do that for you. 似乎您正在尝试从日期字符串中提取年份-没有任何解析方法可以为您完成这一工作。

Instead you should get the actual DateTime represented by the string - July 5th 2000 or May 7th 2000, depending on your format - and then extract the year using DateTime.Year . 相反,您应该获取由字符串表示的实际DateTime 2000年7月5日或2000年5月7日,具体取决于您的格式-然后使用DateTime.Year提取年份。

The highest date that the DateTime can represent is December 31st 9999. You're asking it to understand a year greater than 9999. You either need to specify a format matching the value you're giving it, so that it understands that just the first 4 digits is the year. DateTime可以表示的最高日期是9999年12月31日。您要让它理解大于9999的年份。您要么需要指定一种与您提供的值相匹配的格式,以便它可以理解仅第一个4位数字是年份。 "yyyymmdd", for example. 例如,“ yyyymmdd”。

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

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