none
VS 2017 and DateTime.ParseExact error

    Question

  • Hello.

    I installed new VS2017 and after starting  to work with old web project I faced problems with DateTime.ParseExact function. Irrespective how is the date string formatted I get error "String was not recognized as a valid DateTime" .

    For example:

    // Also tried "2017-01-01"
    string strDATE = "01.01.2017";
    DateTime dt1 = DateTime.ParseExact(strDATE, "dd/MM/yyyy", CultureInfo.InvariantCulture);

    And all this happened after VS 2017 was installed. I have also used different approaches (Convert.DateTime...), but all have the same error message.

    Any thoughts from community?

    Thank you.

    Marko

    Thursday, March 09, 2017 7:57 PM

All replies

  • Try this:

    string strDATE = "01.01.2017";
    DateTime dt1 = DateTime.ParseExact(strDATE, "dd'.'MM'.'yyyy", CultureInfo.InvariantCulture);

    • Marked as answer by MarkoP Friday, March 10, 2017 7:02 AM
    • Unmarked as answer by MarkoP Friday, March 10, 2017 7:28 AM
    Friday, March 10, 2017 6:19 AM
  • Hi Marko,

    Please try the code below:

    string strDATE = "01/01/2017";
    
     DateTime dt1 = DateTime.ParseExact(strDATE, "d/M/yyyy",  null);

    If With the third argument is CultureInfo.InvariantCulture, make sure add using statement System.Globalizaion.

    Hope it helps.

    Best regards,

    Fletch


    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, March 12, 2017 9:00 AM