谁能看到我在这里做错了什么? Assert.IsTrue(parses)
总是失败。
[TestMethod]
public void Can_Parse_To_DateTime()
{
DateTime expected = new DateTime(2011, 10, 19, 16, 01, 59);
DateTime actual;
string value = "Wed Oct 19 16:01:59 PDT 2011";
string mask = "ddd MMM dd HH:mm:ss xxx YYYY";
bool parses = DateTime.TryParseExact(value, mask,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out actual);
Assert.IsTrue(parses);
Assert.AreEqual(expected, actual);
}
我也尝试过,结果相同:
[TestMethod]
public void parsing()
{
DateTime expected = new DateTime(2011, 10, 19, 16, 01, 59);
DateTime actual;
string value = "Wed Oct 19 16:01:59 PDT 2011";
string mask = "ddd MMM dd HH:mm:ss YYYY"; // note removal of "xxx "
value = value.Remove(20, 4); // removal of the "PDT "
bool parses = DateTime.TryParseExact(value, mask,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out actual);
Assert.IsTrue(parses);
Assert.AreEqual(expected, actual);
}
答案 0 :(得分:3)
如Matt Hamilton所述,yyyy
必须是小写的。而xxx
完全无效。您始终可以使用反向方法DateTime.ToString(format,CultureInfo.InvariantCulture)
测试格式字符串。
答案 1 :(得分:1)
string mask = "ddd MMM dd HH:mm:ss PDT yyyy";