How to get all the dates of the previous month in a list

I am looking to find all the dates of the previous month and get each date in a variable using a for each loop.

For Eg: For Jan 2022, I need to get all the dates from 1st Jan 2022 to 31st Jan 2022 in a list and get each date in a variable . Please suggest how i can get it.

1 Like

Hey @mayankjha986

Could you please explain the purpose to suggest better solution…

Thanks
#nK

Hi @mayankjha986 ,

We would need to know if you need to Assign all the Date values to each one variable or if we could keep it as a List/Array/Dictionary.

For Example, Storing the Dates in a List or an Array could be easier.
Let’s consider PreviousMonthsDates be a variable of Type Array(Of DateTime)

Using an Activity we can get the Previous Month dates in the following manner :

PreviousMonthDates = Enumerable.Range(1,now.AddDays(-Now.Day).Day).Select(Function(x)new DateTime(Now.AddMonths(-1).Year,Now.AddMonths(-1).Month,x)).ToArray

Though here we do not have multiple variables for each date value, we can access the Date value using the index in the below way :

PreviousMonthDates(0) //Should Output First Date of the Previous Month

We could Also Store it in a Dictionary in the below way :
Let’s consider PreviousMonthDatesDict is a variable of Type Dictionary(Of Integer, DateTime)

Using an Assign Activity we should be able to Convert the Array to a Dictionary

PreviousMonthDatesDict = new Dictionary(Of Integer,DateTime)  //Initialise Dictionary

PreviousMonthDatesDict = Enumerable.Range(1,now.AddDays(-Now.Day).Day).ToDictionary(Function(x)x,Function(y)new DateTime(Now.AddMonths(-1).Year,Now.AddMonths(-1).Month,y))

We could access the dates by it’s Integer Key corresponding to the Date

PreviousMonthDatesDict(1)   //Should Output the First Date of the Previous Month
1 Like

Hey @mayankjha986 !! It would be better to know your purpose with this to understand your need, but come on!

  • Step 1: Let’s look at the variables. We first create “date_RefDate” which will be our reference variable, which will assume each date of interest to add to our “arr_dates” which will be the array that will store our dates of interest.

  • Step 2: Let’s create our first reference date which is the first of the last month.

  • Step 3: We’re going to add these dates to our array and then we’re going to add a day to them.

The Output:


Hope this helps!

Adding the .xaml.
Main.xaml (8.1 KB)

2 Likes

Hi @mayankjha986

Here is the workflow

Test1.xaml (7.8 KB)

Hope it will helps you

Regards
Gokul

1 Like

Hi @mayankjha986,

You want to get all the dates in a list and variable both dynamically?

Thanks