'String was not recognized as a valid DateTime'Error Occurs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lucindaa
    New Member
    • Dec 2009
    • 11

    'String was not recognized as a valid DateTime'Error Occurs

    Hi,
    i have a string variable having date as following format "05-01-2010"
    when convert this date into DateTime Format i got the following error 'String was not recognized as a valid DateTime'
    Please Help me to solve it
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    I get from my pc a format like that:
    5/1/2010 2:35:08 μμ
    If I had English windows I suppose that instead of "μμ" I would get a "pm".

    Comment

    • ThatThatGuy
      Recognized Expert Contributor
      • Jul 2009
      • 453

      #3
      string date = "05-01-2010";
      Console.WriteLi ne(DateTime.Par se(date));

      This works fine

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You will need to use ParseExact/TryParseExact, to supply the date format.
        For example:
        [code=c#]
        string StringWithDate = "05-01-2010";
        DateTime myDT = DateTime.MinVal ue;
        bool SuccessfullConv ersion=DateTime .TryParseExact( StringWithDate, "MM-dd-yyyy", null, System.Globaliz ation.DateTimeS tyles.None, out myDT);
        //if it returned true, myDT should now be set to correct date
        [/code]

        Try playing around with it some if need be.

        Comment

        Working...