Function to check if the year changes

Asked

Viewed 69 times

2

I have this code that almost works perfectly.

I went to do a test, and when it arrives to generate in January, it generates for the same year, example it generated an invoice for 15/12/2019 and has two more to generate, it generates the correct month, and the wrong year 15/01/2019 and 15/02/2019 and he should change the year. How can I compare?

   for (int i = 1; i <= parcelas; i++)
                {
                    var idfatura = db.FaturaContasReceberP.Max(b => b.Id);

                    var venc = new FaturaContasReceber
                    {
                        FaturaContasReceberPId = idfatura,
                        DataFatura = DateTime.Now,
                        DiasAtraso = totalDia,
                        Data = DateTime.Parse(DateTime.Now.ToString("dd") + "/" + DateTime.Now.AddMonths(i - 1).ToString("MM") + "/" + DateTime.Now.ToString("yyyy")),
                        DataPagamento = datapagamento,
                        Valor = valor1,
                        Total = saldo,
                        Saldo = saldo,
                        Quitado = quitado,
                        Caixa = caixa,
                    };
                    db.Add(venc);
                    db.SaveChanges();
                    saldo = 0;

1 answer

2


If you want to add months to the current date, just do:

Data = DateTime.Now.AddMonths(i - 1);

When you do DateTime.Now.ToString("dd") + "/" + ..., you are creating a string. Next, DateTime.Parse turns this string into a DateTime.

But this whole turn is not necessary, because AddMonths already returns another DateTime with the months added together.

Also, in the string you are mounting, only the month has its value changed, while the year always remains the same. Already using AddMonths, is already automatically verified whether you need to change the year or not.


Just remember that there are some details to consider when adding a month. For example, if the date is January 31, 2019:

var d = new DateTime(2019, 1, 31);
d = d.AddMonths(1);

When adding 1 month, the result would be 31 February. But as February does not have 31 days, the date is adjusted to the last day of the month and the result is 28 February 2019 (if it were in a leap year, the result would be 29 February).


If you have the day value in a separate variable (as per commenting) and want to use the month and current year, just do something like:

public DateTime obterData(string dia) {
    var d = Int32.Parse(dia);
    var now = DateTime.Now;
    // verificar se o valor do dia ultrapassa o último dia do mês
    var ultimoDia = DateTime.DaysInMonth(now.Year, now.Month);
    if (d > ultimoDia) {
        d = ultimoDia;
    }
    return new DateTime(now.Year, now.Month, d);
}

Data = obterData(contrato.DataCobranca.Substring(1, 2)).AddMonths(i - 1);

But if you always want to take the last day of the month, you can create another method to make such an adjustment:

// soma os meses e obtém o último dia do mês resultante
public DateTime UltimoDia(DateTime data, int meses) {
    var d = data.AddMonths(meses);
    return new DateTime(d.Year, d.Month, DateTime.DaysInMonth(d.Year, d.Month));
}

Data = UltimoDia(obterData(contrato.DataCobranca.Substring(1, 2)), i - 1);

But you need to decide whether you’ll always wear the last day or not. For example, if you start on April 30th, will you want the next one to be May 30th or 31st? And when you arrive in February 2020, it will be the 29th, but then in March you go back to 31 or use 29? I believe that here there is a "magic function", you need to decide whether to use the current day or the last day of the month.

  • In this case it works because it takes the current day, but this other case Data = DateTime.Parse(contrato.DataCobranca.Substring(1, 2) + "/" + DateTime.Now.AddMonths(i).ToString("MM") + "/" + DateTime.Now.ToString("yyyy")); takes the substring to know which will generate, there is no way to put only the AddMonths(1)

  • And also the case, if it is the 31st, would have to check which the last day of the month to launch, because not every month has the 31st

  • @marianac_costa I updated the answer

  • In this case it always takes the 30th day, even the 31st month. It is possible to take the 31st day if the month has ?

  • In case he puts T31, it means it will always be the last day of the month, if you only have 30 days it will be the 30th, if you only have 28, it will be the 28th.

  • @marianac_costa This T31 is a parameter/option? If it is, just add a parameter in the function UltimoDia, indicating whether to adjust for the last day of the month. If you don’t have to adjust, just return the result of d.AddMonths(meses). If you have to adjust, use the code above (with new DateTime(d.Year, d.Month, DateTime.DaysInMonth(d.Year, d.Month))).

  • 1

    I’ve done it already, and it worked right, thanks for the help and the explanation.

Show 2 more comments

Browser other questions tagged

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