简体   繁体   中英

C# Best method to convert custom String to DateTime format

What's the best method for converting a custom string to DateTime format?

My custom strings could look any of these... "5d 4h 25m", "4h 25m", or "25m"

You can set any pattern you like and use DateTime.TryParseExact to parse your string to a DateTime object.

You might want to take a look at this page.

var converted = Convert.ToDateTime(yourString);

If it is possible to store each components individually, you could use TimeSpan.

here is a link with details about TimeSpan: http://www.dotnetperls.com/timespan-parse

On a second note, i would be telling you to create a method that takes your string and splits it in its various components and use each components with DateTime or TimeSpan

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