开发者

DateTime ToString() and TryParse()

开发者 https://www.devze.com 2023-03-22 21:48 出处:网络
At first I do: 开发者_StackOverflow中文版 string stringDate = date.ToString(invariantCultureInfo);

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

精彩评论

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