How to loop through dates?

I have an application where I scraped the application date and stored it into a variable. i.e. Date = 1907, format is yyMM as a string.

I need to loop the date from one month prior (i.e 201906 if the Date is 201907) until April of the previous year (i.e 201804).

How would I do this?


You can find the difference between the StartDate and the EndDate and save this as an integer I’ll call TotalDays. Then Use a for each loop for each day in Enumerable.Range(0,TotalDays). Now if you want to loop from oldest date to newest you’d do StartDate.AddDays(day) and if you wanted to loop from newest back to oldest date you’d use EndDate.AddDays(-day)

A simple while loop can accomplish this quite easily. Please see attached:
Main.xaml (6.9 KB)

1 Like

This is very helpful, thank you. What variable types did you use for the startDate and endDate? I also got errors for the Log message because it won’t let a conversion from string to integer occur. How can I remedy this?

Both dates are System.DateTime types. Then I’m logging "Do something with " + startDate.ToString("yyMM") + "." which is just three strings concatenated together. I think fixing the variable type will also fix your conversion error, since the log output should work fine as is.

1 Like

Thanks @DanielMitchell! This worked for me. Follow up question: how can I save these into an array?

1 Like

If you are adding an unknown amount you should add it to a list rather than an array because you can add/remove items from a list, but you can’t do that to an array (you have to resize it instead, which is much slower).

You can add items to a list by using the activity called “add to collection”

1 Like

@Dave I added the add to collection activity and created a variable to store it call AccPeriod. In the Properties panel, I put AccPeriod in the Collection field. I am getting the error: “Complier error(s) encountered processing expression “AccPeriod”. Value of type ‘Date’ cannot be converted to 'System.Collections.Generic.ICollection(Of Object)”.

What need to go in the Collection and Item field?


@happygal321 The properties for the ‘Add To Collection’ activity should be as follows:

Collection: This is the list you are using. It should be a list of system.DateTime - it looks like you named it AccPeriod
Item: This is the datetime variable you want to add to your list.
TypeArgument: Make sure this is System.DateTime

Here is a screenshot showing an example where I have a list of datetime variables called “MyList” and am adding the current datetime to this list.

1 Like

@Dave I changed my variable AccPeriod to of type List and enter that in the properties panel in Collections. But now i get the error “Complier error(s) encountered processing expression “AccPeriod”. Option Strict On disallow implicit conversions from ‘System.Collections.Generic.List(Of Date)’ to ‘System.Collections.Generic.ICollection(Of System.Collections.Generic.List(Of Date))’.”

@happygal321 please double check that you have updated all the properties as shown in the screenshot and listed in my last response. If everything is the same, please delete the activity and try again as sometimes UiPath has trouble when you change variable types like that

@Dave I restarted ui path and deleted and re-added the add collection activity. I am still getting that error.

show me a screenshot that includes the variable pane and the properties pane for that activity and i should be able to point it out

TypeArgument : Make sure this is System.DateTime

Right now it is a list of datetime


Will this work for going forward through dates as well? I am trying to get it to run through dates on the calendar. I am working with TeamUp calendar which works like Google calendar but it just won’t run at all. I’m trying to figure out what is wrong with my automation.

Yes just reverse the order. Currently it is using .AddMonths(-1). Go ahead and change it so it is .AddMonths(1) instead and switch the sign so it is startdate <= enddate


Thank you

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.