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!