【发布时间】:2015-05-24 06:15:40
【问题描述】:
尝试向 .NET 网页添加日期验证(使用 C#)。比较开始日期和结束日期以确保结束日期 > 开始日期。
由于某种原因,我收到以下错误:
字符串未被识别为有效的日期时间。
现在我承认这是我第一次使用 DateTime 方法,所以我可能还没有完全理解它,但据我所知,我的代码应该不错。
谁能告诉我我错过了什么?
String startD = Request["txtStartDate"]; //requesting string from textbox "txtStartDate"
String endD = Request["txtEndDate"];
DateTime start = DateTime.Parse(startD); //line that throws the error
DateTime end = DateTime.Parse(endD);
【问题讨论】:
-
我们可以看到 Request["txtStartDate"] 正在返回吗?
-
请向我们展示
Request["txtStartDate"]和Request["txtEndDate"]的内容。您可以通过调试或控制台输出或任何其他方式获取它们。 -
@TolgaEvcimen 是的,我同意。除非我们看到输入,否则我们什么都做不了
-
用户在 txtStartDate 中输入的值不是 DateTime 数据类型的有效输入。请参阅标准日期和时间格式字符串here
-
假设我正确理解了您的问题,txtStartDate / txtEndDate 请求仅返回格式为 MM/dd/yyyy 的文本字符串。