Doubt sum day in Date Javascript

Asked

Viewed 78 times

0

I am with 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 the txtDataInicio:

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

Where txtDataInicio is filled with the current date, thus "10/16/2017". I’m trying to do the math, today I want to add the total_dias, but it is not working, because it does not recognize the txtDataInicio as a date. For example: If day choice is 20 and current date was 16, 20 - 16 = 4, where it should increase the 4 days in the txtDataInicio. But in every way I try, it reports error, I do not have much experience with dates in JS, I need to add days to the date, but is not getting the correct format. Thank you.

  • "if ($("#<%=txtTipodePlano.Clientid %>").val() == "MONTHLY") {" on this line I believe you are mixing javascript with jquery, it is intentional?

  • @Victorpereira yes, because it’s the way I’m used to doing it. If you have any better suggestions, thank you.

  • "if (Document.getElementById("#<%=txtTipodePlano.Clientid %>").value == "MONTHLY") {" tries to replace by default js, you may not be starting jquery

  • But I did the test by putting a default value in txtVencimentoC, and it enters the right if, it just isn’t doing the account.

  • @marianac_costa does this question help you? Calculate difference between two dates to validate date fields, see that there is a cast in the text before it subtracts the difference between date days.

  • Possible duplicate of Difference between dates

Show 1 more comment

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 in "10/16/2017", you will need to format the date in order 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]);
}

  • I tried this way, but it appears something else in the textbox q I would pass the values, not even in date format. I put 22 in txtVencimento, it is appearing these numbers in txtVencimentoC: 1508675017862

  • Are you using EJS? Could you create a verifiable example (no Engines templates and with all components used) @marianac_costa?

  • I went to check which value is currently in date, this value is appearing "Thu Nov 16 2017 00:00:00 GMT-0200 (Brazilian Daylight Saving Time)" will be that is why he can not do the sum?

Browser other questions tagged

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