简体   繁体   English

为什么DateTime.ParseExact不能解析DateTime输出?

[英]Why can't DateTime.ParseExact parse DateTime output?

While struggling with DateTime.ParseExact formatting issues, I decided to feed ParseExact the out put from DateTime.ToString(), like this: 在努力解决DateTime.ParseExact格式化问题时,我决定从DateTime.ToString()向ParseExact提供输出,如下所示:

DateTime date2 = new DateTime(1962, 1, 27);
string[] expectedFormats = { "G", "g", "f", "F", "D", "d", "M/d/yyy", "MM/dd/yyy", "MM-dd-yyy", "MMM dd, yyy", "MMM dd yyy", "MMMM dd, yyy", "MMMM dd yyy" };
bool parsed = false;

foreach (string fmt in expectedFormats)
{
    try
    {
        DateTime dtDateTime = DateTime.ParseExact(date2.ToString(fmt), fmt, new CultureInfo("en-US"));
        parsed = true;
    }
    catch (Exception)
    {
        parsed = false;
    }

    Console.WriteLine("[{0}] {1}", parsed,date2.ToString(fmt));
}

This is the output: 这是输出:

[True] 1/27/1962 12:00:00 AM
[True] 1/27/1962 12:00 AM
[True] Saturday, January 27, 1962 12:00 AM
[True] Saturday, January 27, 1962 12:00:00 AM
[True] Saturday, January 27, 1962
[True] 1/27/1962
[False] 1/27/1962
[False] 01/27/1962
[False] 01-27-1962
[False] Jan 27, 1962
[False] Jan 27 1962
[False] January 27, 1962
[False] January 27 1962

What do I have to do so that ParseExact will parse the custom format strings? 我必须做什么才能使ParseExact解析自定义格式字符串? Am I wrong to expect DateTime to be able to ingest it's own output based on the same format string? 我错误地期望DateTime能够基于相同的格式字符串摄取它自己的输出吗?

This clearly demonstrates that DateTime.ParseExact is not round-trip safe with Datetime.ToString . 这清楚地表明DateTime.ParseExact不是使用Datetime.ToString的往返安全。 I am not sure this is much of an answer, but the problem is definitely related to the 3 digit year format yyy . 我不确定这是一个很好的答案,但问题肯定与3年级格式yyy Since 1962 cannot be represented in 3 digits ToString is forced to use 4 digits. 自1962年以来不能用3位数表示ToString被强制使用4位数。 Apparently ParseExact is not smart enough to reverse that logic and instead is looking for exactly 3 digits. 显然ParseExact不够智能,无法反转该逻辑,而是正在寻找正好3位数。 The workaround is to use yyyy instead of yyy . 解决方法是使用yyyy而不是yyy I would submit this as a bug to the Microsoft Connect website and see what comes of it. 我会将此作为错误提交给Microsoft Connect网站,看看它是什么来的。

Microsoft has implemented a string format for round-tripping a DateTime. Microsoft已实现了一种字符串格式,用于往返日期时间。 You use the "o" or "O" format. 您使用“o”或“O”格式。

You will need to set the styles parameter in DateTime.ParseExact to DateTimeStyles.RoundtripKind. 您需要将DateTime.ParseExact中的styles参数设置为DateTimeStyles.RoundtripKind。

For full details see: http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx#Roundtrip 有关详细信息,请参阅: http//msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx#Roundtrip

I'm suprised seeing this, but the documentation at msdn says: 看到这个我很惊讶,但msdn的文档说:

You can specify one of the standard date and time format specifiers or a limited combination of the custom date and time format specifier 您可以指定标准日期和时间格式说明符之一,也可以指定自定义日期和时间格式说明符的有限组合

http://msdn.microsoft.com/en-us/library/2h3syy57.aspx http://msdn.microsoft.com/en-us/library/2h3syy57.aspx

Update 更新

You can use the workaround mentioned in the previous answer. 您可以使用上一个答案中提到的解决方法。

I took your code, and changing "yyy" to "yyyy" is enough to return "TRUE" for them all (having first reproduced the "FALSE"s before I made any changes). 我接受了你的代码,将“yyy”改为“yyyy”就足以让所有人都回归“真实”(在我做出任何修改之前首先复制了“FALSE”)。

The documentation at MSDN is unclear, but does emphasize the need for specifying the full width of every component (eg yyyy rather than yyy) without the invariant culture: MSDN上的文档不清楚,但是强调需要在没有不变文化的情况下指定每个组件的全宽(例如yyyy而不是yyy):

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. 如果format是不包含日期或时间分隔符的自定义格式模式(例如“yyyyMMdd HHmm”),请使用provider参数的不变区域性和每个自定义格式说明符的最宽格式。 For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H". 例如,如果要在格式模式中指定小时数,请指定更宽的形式“HH”,而不是更窄的形式“H”。

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

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