We have encountered a special requirement today, which needs to be converted from the following string into
DateTimeObject:
Copy codeThe Code is as follows: [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:Copy codeThe Code is as follows: public static DateTime ParseExact (
String s,
String format,
IFormatProvider provider
)
Example:Copy codeCode: 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, it will throwArgumentNullExceptionException.
• If dateTimeStr or format is a null stringFormatExceptionException.