I have a script that I enter the number of months and returns the date of the deadline. But I need to decrease one day of the result

Asked

Viewed 43 times

-5

Dev’s good afternoon!

I am trying to create a script that calculates the final date from an initial date and its term in months. Ex:

Initial date: 01/08/2018 Deadline: 60 months End date: 31/07/2023 (60 months - 1 day)

function calcDate() {
  var dat = document.getElementById("data").value;
  var meses = document.getElementById("meses").value;
  if(dat != "" && meses != "") {
    var sp = dat.split("/");
    dat = new Date(sp[2], sp[1]-1, sp[0]);
    var m = meses%12;
    var y = Math.floor(meses/12);
    var tmp = dat.setMonth(dat.getMonth()+m);
    var tmp = dat.setYear(dat.getFullYear()+y);
    var f = new Date(tmp);
    document.getElementById("final").value = ("0" + f.getDate()).slice(-2) + "/" + ("0" + (f.getMonth() + 1)).slice(-2) + "/" + f.getFullYear();
  }
}

Thanks in advance.

  • 1

    Good afternoon Pedro, nice that you are trying, put there what you have done so far.

  • 1

    Dear Pedro, understand something, we’re not a free IT service site, you’re asking for something very specific that would have to be developed, and it’s not what we do here, here what we do is help fix scripts, point out API solutions and explain about technologies, terminology. The way your question is this is like you expect someone to really do everything for you, so I recommend you rephrase the question, present what you tried and read https://answall.com/help ... We have rules and recommendations, if you follow them you will get better results.

  • @Leandrade, I edited the post to show how far I went. But I’m still trying...

  • @Guilhermenascimento Thanks! If the question is not pertinent I will be deleting... But I just wanted to inform you that I’m a beginner and I’m seeking help as a way of study.

  • I managed with the help of a friend to create the function that generates the final date. What I’m racking my brain right now is where I need to change it so that the date one day goes down, so that the correct deadline.

1 answer

1


After the variable f

add that line f.setDate(f.getDate()-1);

See example working

function calcDate() {
  var dat = document.getElementById("data").value;
  var meses = document.getElementById("meses").value;
  if(dat != "" && meses != "") {
    var sp = dat.split("/");
    dat = new Date(sp[2], sp[1]-1, sp[0]);
    var m = meses%12;
    var y = Math.floor(meses/12);
    var tmp = dat.setMonth(dat.getMonth()+m);
    var tmp = dat.setYear(dat.getFullYear()+y);
    var f = new Date(tmp);
    //diminui 1 dia da data
    f.setDate(f.getDate()-1);
    document.getElementById("final").value = ("0" + f.getDate()).slice(-2) + "/" + ("0" + (f.getMonth() + 1)).slice(-2) + "/" + f.getFullYear();
  }
}
<input type="text" name="data" id="data" value="01/08/2018"/>
<input type="text" name="meses" id="meses" value="60"/>
<input type="text" name="final" id="final" value=""/>
<button type="button" onclick="calcDate();">Calcular</button>

Another way:

function calcDate() {
  var dat = document.getElementById("data").value;
  var meses = document.getElementById("meses").value;

//Inicialmente formatamos a data para new Date(ano, mes, dia);
partes=dat.split("/");
var myDate=new Date(partes[2],partes[1]-1,partes[0]);
//data final + 60 meses
myDate.setMonth(myDate.getMonth()+parseInt(meses));
//data final - 1 dia
myDate.setDate(myDate.getDate()-1);

var dia=myDate.getDate();
var mes=(myDate.getMonth()+1);

//dia e mes com dois digitos
dia	= (dia.toString().length == 1)?'0'+dia:dia;
mes	= (mes.toString().length == 1)?'0'+mes:mes;

//data final
newDate = dia + "/" + mes + "/" + myDate.getFullYear()

document.getElementById("final").value=newDate;

}
<input type="text" name="data" id="data" value="01/08/2018"/>
<input type="text" name="meses" id="meses" value=""/>
<input type="text" name="final" id="final" value=""/>
<button type="button" onclick="calcDate();">Calcular</button>

Browser other questions tagged

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