- Wed Oct 17, 2018 12:02 am
#817420
Hello,
I have a grid with 2 dates field gFrom - From and gTo - To and also gDuration - Duration (text field). I want difference between gTo-gFrom to be automatically calculated in gDuration filed in round number of days.
I have tried the following code in JavaScript and it doesnt work (form preview doesnt load)
I have a grid with 2 dates field gFrom - From and gTo - To and also gDuration - Duration (text field). I want difference between gTo-gFrom to be automatically calculated in gDuration filed in round number of days.
I have tried the following code in JavaScript and it doesnt work (form preview doesnt load)
Code: Select all
function calcDiff()
{
var totalRows = Number_Rows_Grid("Grid1", "gFrom");
var from, to, diff;
for (var i = 1; i <= totalRows; i++)
{
if (getGridField("Grid1", i, "gFrom") == "" ||
getGridField("Grid1", i, "gTo") == "")
{
getGridField("gDuration").value = "";
continue;
}
from = new Date(getGridField("Grid1", i, "gFrom").value);
to = new Date(getGridField("Grid1", i, "gTo").value);
diff = Math.ceil((to.getTime() - from.getTime()) / 86400000);
getGridField("Grid1", i, "gDuration").value = diff;
}
}
getField("Grid1").onchange = calcDiff;
calcDiff(); //execute when the DynaForm is first displayed