Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help converting this string:

2011-10-20T20:49:59-0400

I need to get the string to look like this :

20:49 10/20/2011

help would be great.
Posted

1 solution

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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900