Days Count of a date [closed]

-1

I needed this parameter to count the days until the date that I type for example (12/31/2016) as shown in the attached image, but it is returning only the number of days it was entered in the 31 day case and not the total days until that date which in the case would be 366 days. Can anyone give me any tips on how to do this count returning the total days until the date entered?

Note: The return of the number of days will be in a label (lblTotalDias.Text)

int iTotal = 0;

if (iDia < iMes)
{
            iDia = 0;
            iTotal++;

            if (iDia < 30)
            {
                iDia++;
                iTotal++;

                if (iMes == 2 && ((iAno % 400 == 0) 
                    || (iAno % 4 == 0 && iAno % 100 != 0)) && iDia == 28)
                {
                    iTotal++;
                    iDia = 31;
                }

                if (iMes == 2 && !((iAno % 400 == 0) 
                    || (iAno % 4 == 0 && iAno % 100 != 0)) && iDia == 27)
                {
                    iTotal++;
                    iDia = 31;
                }

                if ((iMes == 1 || iMes == 3 || iMes == 5 || iMes == 7 
                    || iMes == 8 || iMes == 10 || iMes == 12) && iDia == 30)
                {
                    iDia++;
                    iTotal++;
                }
            }
                iMes++;
                iTotal++;
        }
            iDia++;
            iTotal++;

            lblTotalDias.Text = (iDia - 1) + " DIAS";
  }
  

    
asked by anonymous 15.09.2017 / 23:47

2 answers

2

This can easily be done with DateTime and TotalDays :

DateTime diaEscolhido = new DateTime(iAno, iMes, iDia);  //Cria a variável de datetime com a data escolhida
string dias = ((int)(DateTime.Now - diaEscolhido).TotalDays).ToString(); //Faz a operação para saber a diferença de dias com o dia atual
lblTotalDias.Text = "DIAS: " + dias;

To check if the year is leap year, use IsLeapYear :

lblAnoBissexto.Text = DateTime.IsLeapYear(iAno) ? '366' : '365';

References:

16.09.2017 / 00:31
0

You can first check the year parameter.

//Declarando variáveis que retornaram a diferença de dias
int anofinal, mesfinal, anofinal, total;
//Data que você recolherá
DateTime dt = new DateTime(ano, mes, dia);
//Data que servirá como referência;
DateTimeRef dtref = new DateTime(anoref, mesref, diaref);
if (ano == anoref)
{
   continue;
}
else
{
  total = ((ano - anoref)*365)
}
    
16.09.2017 / 06:48