字符串未被识别为有效的DateTime异常 [英] String was not recognized as a valid DateTime Exception
本文介绍了字符串未被识别为有效的DateTime异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string datestring = txtNewReminderRemindDate.Text.ToString() + " " + RemTime.ToString();
所以我的日期字符串是 17/5/2017 19:10:00
So my datestring is "17/5/2017 19:10:00"
我正在尝试将此字符串转换为放置我的通知。但是当我这样做时:
I'm trying to convert this string to put my Notification. But when I do this:
DateTime alarm = DateTime.ParseExact(datestring, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
我明白了:
未处理的异常:
System.FormatException:字符串未被识别为有效的DateTime。
我不明白我在做什么错。我什至尝试过:
I don't understand what I'm doing wrong. I even tried:
DateTime alarm = DateTime.Parse(datestring);
推荐答案
只需替换 MM
部分为 M
。字符串( 2017/5/17 19:10:00
)中的月份( 5
)只有一位数字,而不是两位数。所以您不应该使用 MM
。
Just replace MM
part with M
. The month (5
) in your string (17/5/2017 19:10:00
) is only one digit, not two digits. So you shouldn't use MM
.
DateTime.ParseExact(datestring, "dd/M/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
这篇关于字符串未被识别为有效的DateTime异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文