Detailed description of how to use the ParseExact method of DateTime to implement special date and time

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.