Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
convert December 17, 2014 to mmddyy in C#
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-15 23:51pm    
This is not even a question.
What's wrong with just reading original MSDN documentation.
—SA
PIEBALDconsult 19-Jan-15 0:11am    
Please leave it as a DateTime until you need to display it, then consider an ISO 8601-compliant format YYYY-MM-DD.

You don't need "convert". Don't try to work with strings representing data, instead of data itself. That said, you need to work with the the type System.DateTime, not strings, and show such objects as strings only when you need to show it on screen (or something like that). Sometimes, you need to parse input string into the instance of DateTime.

Therefore, read the documentation, all the methods Parse, ParseExact, TryParse, TryParseExact and ToString:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].

You can use CultureInfo (implementing the interface IFormatProvider) parameters and/or string parameters to control string formatting. Please see:
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].

—SA
 
Share this answer
 
C#
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0: d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone
String.Format("{0:M/d/yyyy}", dt);            // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"
String.Format("{0:ddd, MMM d, yyyy}", dt);    // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt);  // "Sunday, March 9, 2008"
String.Format("{0:MM/dd/yy}", dt);            // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt);          // "03/09/2008"


Hope this helps
 
Share this answer
 
v2

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