Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Add Dates using loop in Formcalc

Avatar

Level 2

Hi I need help addings dates using for loop in formcalc. I can do manually like this

DateTimeField2.rawValue = Num2Date(Date2num(DateTimeField1.rawValue,"YYYY-MM-DD")+1,"YYYY-MM-DD");

DateTimeField3.rawValue = Num2Date(Date2num(DateTimeField1.rawValue,"YYYY-MM-DD")+2,"YYYY-MM-DD");

DateTimeField4.rawValue = Num2Date(Date2num(DateTimeField1.rawValue,"YYYY-MM-DD")+3,"YYYY-MM-DD");

DateTimeField5.rawValue = Num2Date(Date2num(DateTimeField1.rawValue,"YYYY-MM-DD")+4,"YYYY-MM-DD");

I tryed this for loop:

for i=1 upto 4 do

     var temp = "DateTimeField" + (i+1) + ".rawValue"

     temp= Num2Date(Date2num(DateTimeField1.rawValue,"YYYY-MM-DD")+ i,"YYYY-MM-DD")

endfor

I know this is wrong, my skill in formcalc is very limited any help is very much appreciate

Henry

1 Accepted Solution

Avatar

Correct answer by
Level 10

You need to use instance numbers of the fields instead of trying to use different field names.

So, if all your date fields are called DateTimeField1 they will get instance numbers in brackets after them that you can access: DateTimeField1[0], DateTimeField1[1], etc.

Also, you don't need to use rawValue or semicolons with FormCalc.

So below I'm pulling the data from the first instance of DateTimeField1, which defaults to [0] but I put it in anyway to be sure. The loop will start with DateTimeField[1]:

for i=1 upto 4 do

     DateTimeField1[i] = Num2Date(Date2Num(DateTimeField1[0], "YYYY-MM-DD") + i, "YYYY-MM-DD")

endfor

Sometimes it's good to use variables to make things easier to read, so I'd probably do this as:

for i=1 upto 4 do

  var vDate = Date2Num(DateTimeField1[0], "YYYY-MM-DD")

  DateTimeField1[i] = Num2Date(vDate + i, "YYYY-MM-DD")

endfor

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

You need to use instance numbers of the fields instead of trying to use different field names.

So, if all your date fields are called DateTimeField1 they will get instance numbers in brackets after them that you can access: DateTimeField1[0], DateTimeField1[1], etc.

Also, you don't need to use rawValue or semicolons with FormCalc.

So below I'm pulling the data from the first instance of DateTimeField1, which defaults to [0] but I put it in anyway to be sure. The loop will start with DateTimeField[1]:

for i=1 upto 4 do

     DateTimeField1[i] = Num2Date(Date2Num(DateTimeField1[0], "YYYY-MM-DD") + i, "YYYY-MM-DD")

endfor

Sometimes it's good to use variables to make things easier to read, so I'd probably do this as:

for i=1 upto 4 do

  var vDate = Date2Num(DateTimeField1[0], "YYYY-MM-DD")

  DateTimeField1[i] = Num2Date(vDate + i, "YYYY-MM-DD")

endfor