C # delicious: Use the parseexact method of datetime to parse special date and time

Source: Internet
Author: User
Document directory
  •  
  • References

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

Related Article

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.