none
String to Datetime conversion Failure ParseExact RRS feed

  • Question

  • I'm trying to Parse a String to Datetime. To see the 'datavalue' I added a MessageBox and that shows "09 Feb 2018". Just to further check my syntax, I added another line to See if DateParse is working when I'm hardcoding the value. And over here it's working fine. But in next line when I'm using same code and instead of hardcoded value, I'm passing datavalue.Tostring() then it is throwing exception as

    Friday, February 9, 2018 7:07 AM

Answers

  • Try trim the " at the beginning and the end of datavalue before passing it to DateTime.ParseExact()
    • Marked as answer by AMJEE Friday, February 9, 2018 2:42 PM
    Friday, February 9, 2018 7:16 AM
    Answerer
  • What kind of datatype is 'datavalue'? When it is a string (suggested by  "\"09 Feb 2018\""), then there is no need to apply the ToString() method to it and why did you escape the innermost double quotes? Escaping those quotes might be the reason that the string is not recogniszed as a valid DateTime representation.

    wizend

    • Marked as answer by AMJEE Friday, February 9, 2018 2:42 PM
    Friday, February 9, 2018 2:39 PM

All replies

  • Try trim the " at the beginning and the end of datavalue before passing it to DateTime.ParseExact()
    • Marked as answer by AMJEE Friday, February 9, 2018 2:42 PM
    Friday, February 9, 2018 7:16 AM
    Answerer
  • What kind of datatype is 'datavalue'? When it is a string (suggested by  "\"09 Feb 2018\""), then there is no need to apply the ToString() method to it and why did you escape the innermost double quotes? Escaping those quotes might be the reason that the string is not recogniszed as a valid DateTime representation.

    wizend

    • Marked as answer by AMJEE Friday, February 9, 2018 2:42 PM
    Friday, February 9, 2018 2:39 PM
  • Hello AMJEE,

    String.trim is good choice for you.

    https://msdn.microsoft.com/en-us/library/d4tt83f9.aspx

      string str = "\"asdsdfd\"";
      string result = str.Trim('"');

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 11, 2018 11:20 AM
    Moderator