Search code examples
c#stringdatetimexamarin.android

Convert a short DateTime string with date and time to DateTime


Having trouble with my Android project here trying to convert a string of DateTime into an actual DateTime object. here's the code I wrote:

string strDateTime = reminder.Date + " " + ((ReminderByTime)reminder).ReminderTime;
userDateTime = DateTime.ParseExact(strDateTime, "MM/dd/yyyy HH/mm", null);

While running the problem I've checked the values and they do coordinate: screen shot of debug window

How can I fix this?


Solution

  • When you see Exact in the name of the ParseExact() method, it means it! The method is notoriously unforgiving about small typos or format variations.

    In this case, the time portion of the format string uses a different separator than the actual value. HH/mm, which uses a /, does not match 18:20, which uses a colon (:).

    Therefore your format string needs to look like this:

    MM/dd/yyyy HH:mm
    

    In the future, I also advise you to avoid posting screen shots of technical information like debugger results. I know of no faster way to attract downvotes to your questions, and it's really MUCH better for us to see that kind of information as well-formatted text instead.