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?
– Victor Pereira
@Victorpereira yes, because it’s the way I’m used to doing it. If you have any better suggestions, thank you.
– Mariana
"if (Document.getElementById("#<%=txtTipodePlano.Clientid %>").value == "MONTHLY") {" tries to replace by default js, you may not be starting jquery
– Victor Pereira
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.
– Mariana
@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.
– Marconi
Possible duplicate of Difference between dates
– Marconi