简体   繁体   中英

.NET Parsing string into DateTime

I have a string which is a representation of DateTime:

string dateTime = "20110801";

Now I want to use DateTime.Parse(dateTime);

It throws an Exception stating that dateTime is not in a valid format.

Can anyone tell me how it can be done??

您需要使用格式字符串yyyyMMdd调用ParseExact

You can use DateTime.ParseExact , which takes in the format as a parameter.

For example:

DateTime.ParseExact("20110801", "yyyyMMdd", CultureInfo.CurrentCulture)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM