验证日期以查看日期是否有效时遇到问题 [英] Having a problem validating a date to see if it valid

查看:81
本文介绍了验证日期以查看日期是否有效时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个函数对我有用,但是现在它决定给我一个异常,我试图解决这个异常,但是在尝试验证2个日期方面还没有成功,但是它一直给我一个错误.关于"DateTime dt = Convert.ToDateTime(textbox1.text)"的错误,表明无法识别该字符串,请紧急寻求帮助

非常感谢您的帮助.
代码如下

hi this function has worked for me before but now it decided its going to give me an exception which i have tried to fix but haven''t been successful at it im trying to validate 2 dates, but it keeps giving me a error on "DateTime dt = Convert.ToDateTime(textbox1.text)" that the string was not recognized, please need help with this kinda urgently

help would be greatly appreciated thanks alot.
code as follows

public void DateShow(object sender, EventArgs e)
    {
        try
        {
            DateTime validater = DateTime.Parse(TextBox1.Text);
            DateTime validater2 = DateTime.Parse(TextBox2.Text);
        }
        catch (Exception exc)
        {
            date_Error_cell.Text = exc.Message;
        }
        DateTime dt = Convert.ToDateTime(TextBox1.Text);
        DateTime dta = Convert.ToDateTime(TextBox2.Text);
        TimeSpan pp = dta - dt;
        int days = pp.Days;

        string s = TextBox1.Text;
        string t = TextBox2.Text;

        DateTime theDateA = DateTime.Parse(s);
        DateTime theDateB = DateTime.Parse(t);

       
        if (theDateB > theDateA)
        {
            if (days > 31)
            {
                TextBox1.BackColor = System.Drawing.Color.Red;
                TextBox2.BackColor = System.Drawing.Color.Red;
                date_Error_cell.Text = "Select a DATE NOT more than 31 days";
                date_Error_cell.Attributes.Add("style", "font-weight:bold; text-align:center; font-family:Calibri; color:Red; font-size:large;");
                Monthly_Spend();
                label();
            }
            else
            {

                TextBox1.BackColor = System.Drawing.Color.White;
                TextBox2.BackColor = System.Drawing.Color.White;
                date_Error_cell.Text = "";
                bind_grid();
                Monthly_Spend();
            }
        }
        else
        {
            date_Error_cell.Text = "Select a VALID date";
        }

        

    }

推荐答案

查看您的代码:
Look at your code:
try
{
    DateTime validater = DateTime.Parse(TextBox1.Text);
    DateTime validater2 = DateTime.Parse(TextBox2.Text);
}
catch (Exception exc)
{
    date_Error_cell.Text = exc.Message;
}
DateTime dt = Convert.ToDateTime(TextBox1.Text);
DateTime dta = Convert.ToDateTime(TextBox2.Text);


如果解析失败,则设置一条错误消息,然后继续进行,就好像什么都没发生一样.很自然,如果它未能通过Parse尝试,它将使Convert失败,并引发一个您未捕获的异常.

而是使用 DateTime.TryParse [


If it fails the parse, you set an error message, and then continue as if nothing had happened. So naturally enough, if it failed the Parse attempt, it fails the Convert and throws an exception you don''t catch.

Instead, use DateTime.TryParse[^] and scrap your try...catch block.

DateTime dt;
DateTime dta;
if (!DateTime.TryParse(TextBox1.Text, out dt) || !DateTime.TryParse(TextBox2.Text, out dta))
   {
   date_Error_cell.Text = exc.Message;
   return;
   }
...


您是否正在比以前在另一台计算机上运行代码/程序?如果是,请检查区域设置.

如果更改代码更简单,只需在Convert.ToDateTime
中添加另一个参数即可.
Are you running the code /program on another machine than before? If yes, check the locale settings.

If it is simpler to change the code, just add another parameter to Convert.ToDateTime

System.Globalization.CultureInfo enGB = new System.Globalization.CultureInfo("en-GB");
dt = Convert.ToDateTime( TextBox1.Text, enGB);



希望能有所帮助.如果是这样,请将其标记为解决方案/upvote.

谢谢
Milind



Hope that helps. If it does, mark it as solution/upvote.

Thanks
Milind


这篇关于验证日期以查看日期是否有效时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆