Datetime. tryparseexact Universal Time Format Conversion

Source: Internet
Author: User

This article reprinted: http://blog.csdn.net/gaofang2009/article/details/6073231

The day before yesterday, my colleague asked C # if there is any related method to convert a string like "year, month, day, minute, second" to a datetime object. I have never encountered such a problem, but I have not handled it yet. Do I know if I have, however, it is quite complicated to think about. For example, there are several Representation Methods for the same time "12:30:01:

Reference 20100408123001
20104812301
100408123001
104812301


There may be more. To convert it to a datetime object, you must first understand its format, which is a headache. Later, he found a method: datetime. tryparseexact, using the following:

Program code datetime DT;
Datetime. tryparseexact ("20100408123001", "yyyymmddhhmmss", system. Globalization. cultureinfo. invariantculture, system. Globalization. datetimestyles. adjusttouniversal, out DT );
Console. Write (Dt. tostring ());


It solves the format problem easily: If you want to convert the format, you can tell me the format first! Coincidentally, I used to solve this problem when I deducted resources.

We always want the program to be intelligent and be able to handle everything. When it cannot be so intelligent, we may as well help it, just like the example above: If I want to convert, yes, tell me the format first!

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.