开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜