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;
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 theAddMonths(1)
– Mariana
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
– Mariana
@marianac_costa I updated the answer
– hkotsubo
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 ?
– Mariana
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.
– Mariana
@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 ofd.AddMonths(meses)
. If you have to adjust, use the code above (withnew DateTime(d.Year, d.Month, DateTime.DaysInMonth(d.Year, d.Month))
).– hkotsubo
I’ve done it already, and it worked right, thanks for the help and the explanation.
– Mariana