Day count of a date

Asked

Viewed 331 times

-1

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

Note: The return of the number of days will be on 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";
  }

inserir a descrição da imagem aqui

  • I didn’t understand the criteria that came in 31 days.

  • It wasn’t supposed to be like this: lblTotalDias.Text = (iTotal - 1) + " DIAS";?

  • I typed the date 31/12/2016 and in case I was to return the days of the beginning of the year in the case 01/01/2016 until 31/12/2016 that would be 366 days, but he is not returning me the 366 days as I would like and yes only 31 days that was typed I do not know if could understand

  • @Serve it back to me the 31 days too

  • was for him to calculate the distance in days from the date typed to today’s date?

  • @Drawing was for him to calculate the total days of the date typed for example 31/12/2016 until the initial date of the beginning of the year that would be 01/01/2016 that would be 366 days

  • It lacked to say the clear logic, it is very confused, but, it does not need almost anything of code, the structure Datetime already makes much of these calculations

  • @Virgilionovic Thanks, I took advantage of the code using some rules of day and month and I used the Datetime ai managed to do the calculation without returning error.

Show 3 more comments

2 answers

2

This can be easily done with the DateTime and the 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, use the IsLeapYear:

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

References:

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)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.