none
DateTime.Parse throws error in windows 8.1, 32 bit machine RRS feed

  • Question

  • Hi,

    I have a PC with windows 8.1 and 32 bit 32 bit OS.

    In system, date time format has "." as the separator. I did not observe whether this is the default format or is this customized format.

    With this environment, I have a .net application where I get DateTime value using new DateTime().ToString(). I got the value as "1.1.0001 12.00.00"

    And then after some processing I am converting this value back to datetime using DateTime.Parse() method. Here its throwing the error as invalid string representation. I do not understand why this is happening.

    My question is: string value, that I am trying to convert, is in system date time format because I got this value using the new DateTime() method (so I believe that it is in correct format). But why this is throwing this exception while converting the same value back to system date and time?

    Thanks in advance.

    Thursday, August 13, 2015 10:48 AM

Answers

  • You could use the DateTime.ParseExact method and specify your custom format and a culture. Then it should work:

    string dateString = "1.1.0001 12.00.00";
    DateTime dtObj = DateTime.ParseExact(dateString, "d.M.yyyy hh.mm.ss", System.Globalization.CultureInfo.InvariantCulture);

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Friday, August 21, 2015 4:01 PM

All replies

  • That must be a culture problem. The question is, why do you perform operations on the string value? If you want to do something with the date, you can use the methods from the DateTime object. They are very helpful. So you do not need to parse the string into a DateTime again.

    Maybe you can provide some code of your operations on the string value? Or explain what you want to do?


    Robin Sedlaczek (https://twitter.com/RobinSedlaczek, http://robinsedlaczek.wordpress.com) @ Microsoft Forums (Wave Developments / The Berlin Microsoft Connection - https://www.facebook.com/TheBerlinMicrosoftConnection)

    Thursday, August 13, 2015 11:13 AM
  • Hi mohana t,

    >>With this environment, I have a .net application where I get DateTime value using new DateTime().ToString(). I got the value as "1.1.0001 12.00.00"

    If you want to use DateTime.Parse Method, please make sure you have used the right dataTime format.  From above message, string avlue "1.1.0001 12.00.00" was not recognized as a valid DateTime. So invalid string error throws.

    Here is a msdn document about custom datatime format strings, you'll get some hints form below.

    Custom Date and Time Format Strings

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Friday, August 21, 2015 2:39 AM
    Friday, August 21, 2015 2:35 AM
  • You could use the DateTime.ParseExact method and specify your custom format and a culture. Then it should work:

    string dateString = "1.1.0001 12.00.00";
    DateTime dtObj = DateTime.ParseExact(dateString, "d.M.yyyy hh.mm.ss", System.Globalization.CultureInfo.InvariantCulture);

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Friday, August 21, 2015 4:01 PM