我正在尝试将值传递给 dateTime 对象,但是当月份小于 month10 时出现问题

我正在尝试将日期、月份和年份传递给数据时间对象,但是当值小于第 10 个月时要打印的月份有问题。在我的代码中,我有一个初始日期和算术,我计算了清洁月和通知月。如果该月的最后一天是 30 或 31 以及第 2 个月的特定情况,我也会做必须发生的事情。


这是我的代码:


    int day = (int)DateTime.Now.Day;

    int month = (int)DateTime.Now.Month;

    int year = (int)DateTime.Now.Year;


    int diaLimpeza, mesLimpeza, anoLimpeza;

    int diaNotifica, mesNotifica, anoNotifica;


  public void mes6()

    {

        DateTime dataInicial = new DateTime();

        DateTime dataLimpeza = new DateTime();

        DateTime dataNotifica = new DateTime();


        day = 31;

        month = 12;

        year = 2019;


        mesLimpeza=  month + 6;

        if (mesLimpeza > 12)

        {

            mesLimpeza = mesLimpeza - 12;

            anoLimpeza = year + 1;

            diaLimpeza = day;


        }

        else

        {

            mesLimpeza = month + 6;

            anoLimpeza = year;

            diaLimpeza = day;

        }


        int soma = diaLimpeza + 2;


        diaNotifica = soma;


        if (mesLimpeza == 01 || mesLimpeza == 03 || mesLimpeza == 05 || mesLimpeza == 07 || mesLimpeza == 08 || mesLimpeza == 10) //mesLimpeza == 12 excepçao

        {

            if (diaLimpeza == 30 || diaLimpeza == 31)

            {

                diaNotifica = diaNotifica - 31;

                mesNotifica = mesLimpeza + 1;

                anoNotifica = anoLimpeza;

                //int a = diaNotifica;

            }


            else if ((diaLimpeza == 30 || diaLimpeza == 31) && mesLimpeza == 12)

            {

                diaNotifica = diaNotifica - 31;

                mesNotifica = (mesLimpeza + 1) - 12;

                anoNotifica = anoLimpeza + 1;

            }

            else

            {

                diaNotifica = diaLimpeza + 2;

                mesNotifica = mesLimpeza;

                anoNotifica = anoLimpeza;

            }

        }

在最后一行,我试图在文本框中显示日期格式,如 (yyyy-MM-dd)。


我模拟了一个初始日、月和年,当月的结果等于或大于 10 时,一切都很好。


当值较小时,图像上出现此错误:


我的错误

我注意到月份格式不接受小于 10 且没有零的数字,例如月份“6”,我必须输入“06”以使其格式正确。


我怎么解决这个问题?


我在 visual studio 中使用 C#。


翻过高山走不出你
浏览 108回答 3
3回答

繁华开满天机

你似乎在以艰难的方式做事:textBoxData.Text = (new DateTime(year, month, day).AddMonths(6).AddDays(2)).ToShortDateString();应该做的伎俩。让对象为您计算日期。

函数式编程

您的最终日期无效,您正在创建一个日期,月份 = 6(6 月),日期 = 31。但是 6 月没有第 31 天。我添加了一行代码if (diaLimpeza > 30) diaLimpeza--;else if (mesLimpeza == 04 || mesLimpeza == 06 || mesLimpeza == 09 || mesLimpeza == 11)减去一天后。

Cats萌萌

使用此代码,当用户在组合框中选择 6 个月时,我会计算开始日期、清理日期和通知日期。我也看看这一年是否是闰年。小心02月的考试。如果不是闰年并且你用第 29 天进行测试,你会得到一个错误,因为第 29 天在二月不存在,而当年是闰年。&nbsp; &nbsp; int day = (int)DateTime.Now.Day;&nbsp; &nbsp; int month = (int)DateTime.Now.Month;&nbsp; &nbsp; int year = (int)DateTime.Now.Year;&nbsp; &nbsp; int diaLimpeza, mesLimpeza, anoLimpeza,diaNotifica, mesNotifica, anoNotifica;&nbsp; public void mes6()&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DateTime dataInicial = new DateTime();&nbsp; &nbsp; &nbsp; &nbsp; DateTime dataLimpeza = new DateTime();&nbsp; &nbsp; &nbsp; &nbsp; DateTime dataNotifica = new DateTime();&nbsp; &nbsp; &nbsp; &nbsp; day = 28;&nbsp; &nbsp; &nbsp; &nbsp; month = 02;&nbsp; &nbsp; &nbsp; &nbsp; year = 2018;&nbsp; &nbsp; &nbsp; &nbsp; mesLimpeza=&nbsp; month + 6;&nbsp; &nbsp; &nbsp; &nbsp; if (mesLimpeza > 12)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesLimpeza = mesLimpeza - 12;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoLimpeza = year + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaLimpeza = day;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesLimpeza = month + 6;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoLimpeza = year;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaLimpeza = day;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (mesLimpeza == 01 || mesLimpeza == 03 || mesLimpeza == 05 || mesLimpeza == 07 || mesLimpeza == 08 || mesLimpeza == 10 || mesLimpeza == 12) //mesLimpeza == 12 excepçao&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza == 30 || diaLimpeza == 31)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = (diaLimpeza + 2) - 31;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if ((diaLimpeza == 30 || diaLimpeza == 31) && mesLimpeza == 12)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = (diaLimpeza + 2)-31;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = diaLimpeza + 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mesLimpeza == 04 || mesLimpeza == 06 || mesLimpeza == 09 || mesLimpeza == 11)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza > 30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaLimpeza = 30;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza == 29 || diaLimpeza == 30)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = (diaLimpeza + 2) - 30; //mudei esta linha&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = diaLimpeza + 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (mesLimpeza == 02)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (anoLimpeza % 4 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza == 30 || diaLimpeza == 31)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaLimpeza = 29;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza == 28 || diaLimpeza == 29)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = (diaLimpeza + 2) - 29;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza >= 28 && diaLimpeza <= 31)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaLimpeza = 28;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (diaLimpeza == 27 || diaLimpeza == 28)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = (diaLimpeza + 2) - 28;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diaNotifica = diaLimpeza + 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mesNotifica = mesLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; anoNotifica = anoLimpeza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; dataInicial = new DateTime(year, month, day);&nbsp; &nbsp; &nbsp; &nbsp; dataLimpeza = new DateTime(anoLimpeza,mesLimpeza, diaLimpeza);&nbsp; &nbsp; &nbsp; &nbsp; dataNotifica = new DateTime(anoNotifica, mesNotifica, diaNotifica);&nbsp; &nbsp; &nbsp; &nbsp; textBoxData.Text = dataInicial.ToShortDateString() + "\r\n" + dataLimpeza.ToShortDateString() + "\r\n" + dataNotifica.ToShortDateString();&nbsp; &nbsp; }&nbsp;private void guardaStock_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (comboLimpeza.Text == "6 Meses")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mes6();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP