Có ai có thể nhìn thấy những gì tôi đang làm sai ở đây không? Assert.IsTrue(parses)
luôn thất bại.Tại sao phân tích DateTime này luôn thất bại?
[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);
}
Tôi cũng đã thử nó như vậy, với kết quả tương tự:
[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);
}
Năm phải là "yyyy" (chữ thường), nhưng tôi không chắc có chuỗi định dạng nào sẽ diễn giải "PDT". –
@MattHamilton - đó là câu trả lời! thay đổi nó và nó hoạt động. Đăng như một câu trả lời và tôi sẽ chấp nhận nó. –
Hãy để @Al có đại diện. :) –