Document directory
Today, we have encountered a special requirement. We need to convert the string below into a datetime object:
[15:50:42]
The main problem is that this time is not a standard time, but a custom format, starting with month-day and then time.
The most common datetime. parse (string datetimestr) cannot be converted. The problem lies in the custom format.
After searching, I found the following method:
Public static datetime parseexact (string S, string format, iformatprovider provider)
Example:
VaR datetimestr = "07-13 15:50:42"; var datetime = datetime. parseexact (datetimestr, "Mm-dd hh: mm: SS", cultureinfo. invariantculture );
The effect is as follows:
If you use other special languages, such as the United States or Japanese, you may need to obtain the corresponding culture for the final parameter.
Note:
- If datetimestr or format is null, an argumentnullexception exception is thrown.
- If datetimestr or format is a Null String, A formatexception is thrown.
References
Datetime. parseexact method (string, String, iformatprovider)
Http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx