locked
Converting a date string to Month day, year RRS feed

  • Question

  • How to you convert a date (such as "2/2/2012") to February 2, 2012?
    DB Architect
    Thursday, February 2, 2012 9:26 PM

Answers

  • Hi Ben Aminnia,
    Try:

    DateTime date = DateTime.Parse("2/2/2012");
    string formattedDate = date.ToString("MMMM d, yyyy");
    Debug.WriteLine(formattedDate);
    

    Please see this Msdn article for more about custom date formats.


    Currently developing FaultTrack. I occassionally blog about C# and .NET.
    Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com
    • Proposed as answer by Pantelis44999 Thursday, February 2, 2012 9:55 PM
    • Marked as answer by Ben Aminnia Thursday, February 2, 2012 10:02 PM
    Thursday, February 2, 2012 9:51 PM

All replies

  • DateTime.Parse();

    Note that your current culture settings will probably play a role in determining if the month or day comes first.

    • Proposed as answer by servy42 Thursday, February 2, 2012 9:54 PM
    Thursday, February 2, 2012 9:41 PM
  • Hi Ben Aminnia,
    Try:

    DateTime date = DateTime.Parse("2/2/2012");
    string formattedDate = date.ToString("MMMM d, yyyy");
    Debug.WriteLine(formattedDate);
    

    Please see this Msdn article for more about custom date formats.


    Currently developing FaultTrack. I occassionally blog about C# and .NET.
    Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com
    • Proposed as answer by Pantelis44999 Thursday, February 2, 2012 9:55 PM
    • Marked as answer by Ben Aminnia Thursday, February 2, 2012 10:02 PM
    Thursday, February 2, 2012 9:51 PM
  • Thanks
    DB Architect
    Thursday, February 2, 2012 10:02 PM
  • Hi Ben Aminnia,

    Date must be fixed digits, 20051126 is 8 bits, 20050203 cannot so said into 200523

    //For the 20051126 respectively day, month and year string

    string yyyy= "20051126 ".Substring(0,4);
    string mm= "20051126 ".Substring(4,2);
    string dd= "20051126 ".Substring(5,2);

     //spelling conform to date format string
     string riqi=yyyy+ "- "+mm+ "- "+dd;

     

    // will be eligible to date format string into DateTime data types

     DateTime dt=Convert.ToDateTime(riqi);


     Sincerely,

    Jason Wang


    orichisonic http://blog.csdn.net/orichisonic
    Friday, February 3, 2012 1:49 AM