Skip to main content
Participant
July 13, 2007
Question

Calculate how many days between two dates (within repeat region)

  • July 13, 2007
  • 5 replies
  • 385 views
What to do to make the function below works within a repeat region? Each line has a different value for "date1".

function calcDays(){
with(document.form1){
d1=date1.value.split("/");
d2=date2.value.split("/");
d1_dato=new Date(d1[2],d1[1]-1,d1[0]);
d2_dato=new Date(d2[2],d2[1]-1,d2[0]);
days.value=Math.round((d2_dato-d1_dato)/(1000*60*60*24))
}
}

...

<%
While ((Repeat1__numRows <> 0) AND (NOT RSticket.EOF))
%>
<tr>
<td><div align="center">
<input name="date1" type="text" id="date1" value="<%=(RSticket.Fields.Item("vencto").Value)%>" size="8" />
<input name="date2" type="text" id="date2" value="<%= date %>" size="8" />
</div></td>
<td><input name="dias" type="text" id="dias" onfocus="calcData()" size="3" /></td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
RSticket.MoveNext()
Wend
%>
This topic has been closed for replies.

5 replies

e_barAuthor
Participant
July 20, 2007
pls, I need help. tks in advance.
e_barAuthor
Participant
July 18, 2007
Ok, one error solved but now I get:
line: 14, char: 1, error: 'date1' is undefined

The line: d1=date1.value.split("/");
Inspiring
July 17, 2007
e.bar wrote:

> I received two script errors:
>
> line 17, char 16, syntax error, code 0: (when page loads)


days+rowID.value=Math.round((d2_dato-d1_dato)/(1000*60*60*24))

elements[days+rowID].value=Math.floor((d2_dato-d1_dato)/86400000);


Mick.

>
> line 164, char 1, object expected, code 0: (when try to get result on field
> "days")
> <td><input name="days<%=Repeat1__index%>" type="text"
> id="days<%=Repeat1__index%>"
>
>
e_barAuthor
Participant
July 16, 2007
I received two script errors:

line 17, char 16, syntax error, code 0: (when page loads)
days+rowID.value=Math.round((d2_dato-d1_dato)/(1000*60*60*24))

line 164, char 1, object expected, code 0: (when try to get result on field "days")
<td><input name="days<%=Repeat1__index%>" type="text"
id="days<%=Repeat1__index%>"
Inspiring
July 16, 2007
You need to make date1 unique for each row. The simplest way to do this is
to add a unique number (variable) to each form element name.
I often create a variable called cnt and increase this on each loop. However
you could always use the Repeat1__index already there...

You than need to padd this to your function so the JS knows which value to
calculate.

Something like the following should do the trick:

function calcData(rowID){
with(document.form1){
d1=date1+rowID.value.split("/");
d2=date2+rowID.value.split("/");
d1_dato=new Date(d1[2],d1[1]-1,d1[0]);
d2_dato=new Date(d2[2],d2[1]-1,d2[0]);
days+rowID.value=Math.round((d2_dato-d1_dato)/(1000*60*60*24))
}
}

...

<%
While ((Repeat1__numRows <> 0) AND (NOT RSticket.EOF))
%>
<tr>
<td><div align="center">
<input name="date1<%=Repeat1__index%>" type="text"
id="date1<%=Repeat1__index%>"
value="<%=(RSticket.Fields.Item("vencto").Value)%>" size="8" />
<input name="date2<%=Repeat1__index%>" type="text"
id="date2<%=Repeat1__index%>" value="<%=
date %>" size="8" />
</div></td>
<td><input name="days<%=Repeat1__index%>" type="text"
id="days<%=Repeat1__index%>"
onfocus="calcData('<%=Repeat1__index%>')" size="3" /></td>
</tr>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
RSticket.MoveNext()
Wend
%>