首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DateTime.Compare验证

DateTime.Compare验证
EN

Stack Overflow用户
提问于 2015-07-26 03:54:07
回答 1查看 114关注 0票数 0

我一直在DateTime.ParseExact的DateTime.ParseExact验证代码中得到这个错误,并且不知道问题出在哪里。

“字符串不能被识别为有效的DateTime。”

字符串startDate和endDate是从需要验证的文本字段输入的,startDate不能大于EndDate。如果用户按下提交按钮(如果日期输入不正确),则应引发错误。

代码语言:javascript
复制
 String startDate = Request["txtStartDate"];
    String endDate = Request["txtEndDate"];
    DateTime start = DateTime.ParseExact(startDate, "MM/dd/yyyy",
                           System.Globalization.CultureInfo.InvariantCulture);
    DateTime end = DateTime.ParseExact(endDate, "MM/dd/yyyy",
                        System.Globalization.CultureInfo.InvariantCulture);
    if (DateTime.Compare(start, end) > 0)
    {
        txtStartDate.BackColor = System.Drawing.Color.Yellow;
        txtEndDate.BackColor = System.Drawing.Color.Yellow;
        Msg = " The end date must be a later date than the start date. ";
        lblError.Text += Msg;
        validatedState = false;

    }
    else
    {
        txtStartDate.BackColor = System.Drawing.Color.White;
        txtEndDate.BackColor = System.Drawing.Color.White;

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-26 21:19:19

您需要使用DateTime.TryParseExact来首先测试传递的日期时间字符串是否有效。然后,如果错误消息未通过验证,则可以提供它。

代码语言:javascript
复制
        String startDate = Request["txtStartDate"];
        String endDate = Request["txtEndDate"];

        DateTime start;
        if (!DateTime.TryParseExact(startDate, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out start))
        {
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
            Msg = " The start date is a invalid format.";
            lblError.Text += Msg;
            validatedState = false;
            return;
        }

        DateTime end;
        if (!DateTime.TryParseExact(endDate , "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out end))
        {
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
            Msg = " The end date is a invalid format.";
            lblError.Text += Msg;
            validatedState = false;
            return;
        }
        if (DateTime.Compare(start, end) > 0)
        {
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
            Msg = " The end date must be a later date than the start date. ";
            lblError.Text += Msg;
            validatedState = false;

        }
        else
        {
            txtStartDate.BackColor = System.Drawing.Color.White;
            txtEndDate.BackColor = System.Drawing.Color.White;

        }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31633465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档