First thing is: think about taking care of required format in first place; re-formatting of anything is usually a sign of someone's mistake, but I understand that maybe not yours.
Second thing: your input format is one of the best as ordering in time is the same as alphanumeric, so think about keeping to use it.
Third thing: try to work with
System.DateTime
, not with strings. A string is only needed when you present a final result on screen or somewhere else.
Finally, how to reformat? The idea is: parse input string as
System.DateTime
, work with
System.DateTime
all the time, and when you need to present the results as a string, use one of the method
System.DateTime.String
, specifying appropriate format. Sometime you need explicitly specified format, but more often it should be based on current thread culture, sometimes of explicitly specified culture.
For parsing, use one of the methods
System.DateTime.Parse, System.DateTime.ParseExact
, for formatting: one of the
System.DateTime.ToString
methods.
Pay attention for the code sample showing how to use
CultureInfo
as
IFormatProvider
:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[
^].
Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[
^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[
^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[
^].
Pay attention that some standard formats produce different results for different cultures; and the culture used is defined by the current culture of the calling thread. To change the culture of the thread, use
System.Threading.Thread.CurrentCulture
, please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx[
^].
—SA