Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading