简体   繁体   English

DateTime.TryParseExact仅在“单向”中工作

[英]DateTime.TryParseExact only working in “One Way”

Scope: 范围:

I have been trying to develop a super-tolerant DateTime.Parse routine, so I decided to give most "widely-used" formats a try to better understand the format masks . 我一直在尝试开发一个super-tolerant DateTime.Parse例程,因此我决定尝试使用大多数“广泛使用”的格式来更好地理解format masks

Problem: 问题:

I have defined a specific format (String) which I use as myDate.ToString(format) , and it works wonders. 我已经定义了一个特定的格式(String),我用它作为myDate.ToString(format) ,它可以myDate.ToString(format)奇迹。 The problem is, If I get this same String (result of the .ToString(format) operation), and feed it back to DateTime.TryParseExact (...) it fails. 问题是,如果我得到相同的String( .ToString(format)操作的结果),并将其反馈给DateTime.TryParseExact (...)则失败。

Code / Test: 代码/测试:

System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

// Defining Format and Testing it via "DateTime.ToString(format)"
string format = "MM/dd/yyyy HH:mm:ss tt"; 
string dtNow  = DateTime.Now.ToString (format);
Console.WriteLine (dtNow);

// Trying to Parse DateTime on the same Format defined Above
DateTime time;
if (DateTime.TryParseExact (dtNow, format, provider,    System.Globalization.DateTimeStyles.None, out time))
{
    // If TryParseExact Worked
    Console.WriteLine ("Result: " + time.ToString ());  
}
else
{
    // If TryParseExact Failed
    Console.WriteLine ("Failed to Parse Date");
}

Output is : "Failed to Parse Date". 输出为: "Failed to Parse Date".

Question: 题:

Why can I use the format string to format a certain date as text, but I can't use the same format to feed the string back to a date object ? 为什么我可以使用format字符串将某个日期格式化为文本,但是我不能使用相同的format将字符串反馈给日期对象?

EDIT: 编辑:

I have added part of my method to this example , and I would like to know why the "ParseDate" method fails to return a proper date, given that the "String" is in the right format. 我已将此方法的一部分添加到此示例中 ,并且我想知道为什么“ParseDate”方法无法返回正确的日期,因为“String”的格式正确。

Since you use DateTime.ToString() method without any IFormatProvider , this method will use your CurrentCulture settings. 由于您使用DateTime.ToString()方法而没有任何IFormatProvider ,因此此方法将使用您的CurrentCulture设置。

That's why your 这就是你的原因

string dtNow  = DateTime.Now.ToString (format);

line might generate a different string representation than MM/dd/yyyy HH:mm:ss tt format. line 可能会生成与MM/dd/yyyy HH:mm:ss tt格式不同的字符串表示形式。

Three things can cause this issue; 有三件事可能导致这个问题;

Since you try to parse your string with provider (which is InvariantCulture ) on your DateTime.TryParseExact method, generate your string based on that provider as well. 由于您尝试使用DateTime.TryParseExact方法的provider (即InvariantCulture )解析字符串,因此也要根据该提供程序生成字符串。

string dtNow  = DateTime.Now.ToString(format, provider);

You told your CurrentCulture is pt-BR and this culture has empty string "" as a AMDesignator and PMDesignator . 你告诉你的CurrentCulturept-BR ,这种文化有空字符串""作为AMDesignatorPMDesignator That's why your dtNow string will not have any AM or PM designator on it's representation part. 这就是为什么你的dtNow字符串在其表示部分没有任何AM或PM指示符的原因。

Here a demonstration . 这是一个demonstration

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

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