Page 1 of 1

Generating Dates for the Last 30 Days

Posted: Tue Apr 04, 2017 9:34 pm
by KMac21
I want to generate a list of dates to pass to a URL for a total of 30 days ending with yesterday's date in the format YYYY-MM-DD. I don't know CTL that well so it probably can be done withing a DataGenerator by starting with today - 1 then keep subtracting 1 day until you have a total of 30 days, but I'm not sure of the syntax. Any suggestions would be appreciated!

Re: Generating Dates for the Last 30 Days

Posted: Thu Apr 06, 2017 3:20 pm
by krizovav
Hi,

If you would like to receive a list of dates in one record, it has to be set in metadata at first. Set the Type to “date”, Container type to “list” and Format to “yyyy-MM-dd”, please. Then you can use the code below in DataGenerator.

Code: Select all

function integer generate() {   
   
   date[] list1;
   
   for(integer i = 1; i <= 30; ++i) {
         list1 = push(list1, dateAdd(today(), -i, day));
   }
      
   $out.0.field1 = list1;
   
   return ALL;
}


If you prefer a single record for each date (30 records), put the Normalizer after the DataGenerator and use the code below in it. It will convert multivalue field to multiple records.

Code: Select all

function integer count() {
   
   return length($in.0.field1);
}


function integer transform(integer idx) {

   $out.0.field1 = $in.0.field1[idx];
   return OK;
}


I hope this helps.

Regards,

Re: Generating Dates for the Last 30 Days

Posted: Thu Apr 06, 2017 7:13 pm
by KMac21
This was exactly what I was looking for. Thank you for your help!