Doubt sum day in JavaScript Date

0

I have this function in JS:

now = new Date;
var dia_atual = now.getDate();
var atual_data = new Date(document.getElementById("<%= txtDataInicio.ClientID %>").value);
var dia_escolha = document.getElementById("<%= txtDiaVencimento.ClientID %>").value;
if ($("#<%=txtTipodePlano.ClientID %>").val() == "MENSAL") {
	if (parseInt(dia_escolha) > parseInt(dia_atual))
	{
		var total_dias = dia_escolha - dia_atual;
		var outraData = new Date();
		
		document.getElementById("<%= txtVencimentoC.ClientID %>").value = outraData.setDate(atual_data.getDate() + total_dias);
	}
}

This is txtDataHome:

<asp:TextBox ID="txtDataInicio" runat="server" onBlur="limparDataInvalida(this);" class="form-control"></asp:TextBox>

Where txtDataInicio is filled with the current date, this way "10/16/2017". I'm trying to make the account, today I want to add total_dias , but it is not working, because it does not recognize txtDataInicio as date. For example: If day of choice is 20 and current date was 16 , 20 - 16 = 4 , where it should increase the 4 days in txtDataInicio . However of all the ways that I try, it informs error, I do not have much experience with dates in JS , I need to add days to the date, but it is not taking the correct format. Thanks.

    
asked by anonymous 16.10.2017 / 13:36

1 answer

0

The problem is the date format. This new Date(document.getElementById("<%= txtDataInicio.ClientID %>").value); will return you null if the format is on "10/16/2017", you will need to format the date to be able to do operations with it.

var now = new Date;
var dia_atual = now.getDate();
var atual_data = toDate('16/10/2017');

console.log(atual_data);

function toDate(texto){
  let partes = texto.split('/');
  return new Date(partes[2], partes[1], partes[0]);
}
    
16.10.2017 / 13:53