C# 对日期进行控制

C# 对日期进行控制,c#,C#,我试图在文本框中插入一个日期,并制作一些控件,例如,不要让用户输入一个尚未到来的日期,但它会抛出一个异常 string[] array = txtData.Text.Split('/'); int[] arrayint = new int[3]; arrayint[0] = Convert.ToInt32(array[0]); arrayint[1] = Convert.ToInt32(array[1]); arrayint[2] = Convert.ToInt32(array[2]); try

我试图在文本框中插入一个日期,并制作一些控件,例如,不要让用户输入一个尚未到来的日期,但它会抛出一个异常

string[] array = txtData.Text.Split('/');
int[] arrayint = new int[3];
arrayint[0] = Convert.ToInt32(array[0]);
arrayint[1] = Convert.ToInt32(array[1]);
arrayint[2] = Convert.ToInt32(array[2]);
try
{
    if (arrayint[0] > 0 && arrayint[0] < 32)
    {
        if (arrayint[1] > 0 && arrayint[1] < 13)
        {
            if (DateTime.Compare(DateTime.Today, Convert.ToDateTime(txtData.Text)) <= 0)
            {
                string s = txtTitolo.Text + ", " + txtData.Text + ", " + txtDim.Text + ", " + txtFormato.Text + ", " + txtRisoluzione.Text;
                listVideo.Items.Add(s);
            }
        }
    }
}
catch (Exception)
{
    MessageBox.Show("data non valida");
}
感谢所有回答的人,请尝试以下答案:

DateTime dateTime;
if (DateTime.TryParseExact(txtData.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
    //conversion succeeded, you date is in dateTime var
}
else
{
    //conversion failed
}
编辑:


我就是这样改变的

DateTime dateTime;
if (DateTime.TryParseExact(txtData.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
    MessageBox.Show("date has been added");
    string s = txtTitolo.Text + ", " + txtData.Text + ", " + txtDim.Text + ", " + txtFormato.Text + ", " + txtRisoluzione.Text;
    listVideo.Items.Add(s);
}
else
{
    MessageBox.Show("invalid date");
}

您可以传递一组可接受的格式,如下所示:

        DateTime dateTime;
        string[] formats = { "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/M/yy", "dd/M/yy", "d/MM/yy", "dd/MM/yy" };
        if (DateTime.TryParseExact(txtData.Text, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
        {
            MessageBox.Show("date has been added");
            string s = txtTitolo.Text + ", " + txtData.Text + ", " + txtDim.Text + ", " + txtFormato.Text + ", " + txtRisoluzione.Text;
            listVideo.Items.Add(s);
        }
        else
        {
            MessageBox.Show("invalid date");
        }

这就是现在的样子

string date = txtData.Text;
                DateTime dt = DateTime.ParseExact(date,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentCulture);
                if (DateTime.Compare(dt, DateTime.Now) > 0)
                    MessageBox.Show("data non valida");
                else
                {
                    MessageBox.Show("date has been added");
                    string s = txtTitolo.Text + ", " + txtData.Text + ", " + txtDim.Text + ", " + txtFormato.Text + ", " + txtRisoluzione.Text;
                    listVideo.Items.Add(s);
                }

此外,每当我试图在xls文件中打印列表时,它不会打印数据,只打印其他4个值,因此它看起来像:title | | size | format | resolution

到底有什么例外?在哪一条线上?你目前的文化是什么?你能说得更具体一点吗?例外是想告诉你问题出在哪里。不要忽略它。第一个变化,至少是这样的:catch Exception ex{MessageBox.Showex;}您所做的工作超出了您的需要。只需使用DateTime.TryParse并去掉将各个部分转换为整数的代码…它会在convertit上出现异常它总是说无效的datewhat's in txtData.Text,准确吗?例如:2018年6月22日或2012年6月22日。请确保您在txtData中有正确的数据。文本因为我刚刚在代码中粘贴了2018年6月22日,它可以正常工作。查看我的编辑它总是提供我在其他文件上输入的内容。因此,如果您的输入不是数组中列出的格式之一,或者它们是该格式,但例如无效日期,大于该月天数的日值。让MessageBox向您显示无效日期和/或将其输出到控制台,以便您可以看到发生了什么。。。
string date = txtData.Text;
                DateTime dt = DateTime.ParseExact(date,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentCulture);
                if (DateTime.Compare(dt, DateTime.Now) > 0)
                    MessageBox.Show("data non valida");
                else
                {
                    MessageBox.Show("date has been added");
                    string s = txtTitolo.Text + ", " + txtData.Text + ", " + txtDim.Text + ", " + txtFormato.Text + ", " + txtRisoluzione.Text;
                    listVideo.Items.Add(s);
                }