String to datetime problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    String to datetime problem

    i've got a string like

    "May 21 2011"

    how do i get it in the yyyy/mm/dd format

    i've tried

    Code:
    public DateTime dt;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            string date = Request.QueryString["date"];
            
            dt = new DateTime();
            dt = DateTime.ParseExact(date, "MM-dd-yyyy", null);
    
        }
    And i looked on google for ages but I didn't find anything.

    Please help
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Have a look at this page: http://www.csharp-examples.net/string-format-datetime/

    These are the C# date/time format codes and you can use these to parse your data. Here's a sample I made based on what you've said you're looking for...

    Code:
    string dateStr = "May 21 2011";
    DateTime dt = DateTime.ParseExact(dateStr, "MMMM dd yyyy", null);
    MessageBox.Show(dt.ToString("yyyy/MM/dd"));

    Comment

    • Sl1ver
      New Member
      • Mar 2009
      • 196

      #3
      Hahaha....Works perfectly. Thank for your help

      Comment

      Working...