简体   繁体   中英

C# , How can i build a DateTime object from a datetime present in a string object?

在C#中,如何从字符串对象中存在的日期时间(字符串strDate =“ 2009/11/11”)构建DateTime对象?

Have a look at the DateTime.Parse or DateTime.ParseExact methods.

Simplest case:

var result = DateTime.Parse(str);

There's also DateTime.ParseExact which is also worth knowing about.

There's also TryParse and TryParseExact which do the same thing, but won't throw an exception if the date is not valid - will just return a bool.

Using that string format will give you a lot of issues later, such as what is "03/04/2009"?

try using DateTime.ParseExact .

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