DateTime ToString() and TryParse()
At first I do:
开发者_StackOverflow中文版string stringDate = date.ToString(invariantCultureInfo);
Then:
DateTime.TryParse(stringDate, out date);
and it returns false. Do you have any ideas?
Try this:
DateTime.TryParse(stringDate, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
InvariantCulture
did not work for me. I took a DateTime
from a SQL Server database and converted it to a string by using the ToString()
method of the DateTime
object. Then I attempted DateTime.TryParse()
on that very same string and it failed. Further testing showed that TryParse()
was failing because of the hyphen that ToString()
inserts between the date element and the time element.
精彩评论