简体   繁体   中英

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:调试窗口的屏幕截图

How can I fix this?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM