Specify Weekdays Range

I want to get a weekday of the previous month and cut it into 1 week and tie it in a range.
I’m thinking about range (Monday: Friday), but I’m worried about the first and last weeks.

Use the date manipulation for this.

``````1. Assign: currentDate = DateTime.Now
3. Assign: weekdayOfFirstDay = firstDayOfPreviousMonth.DayOfWeek
4. Assign: daysToMonday = (weekdayOfFirstDay - DayOfWeek.Monday + 7) % 7
6. Assign: endDate = startDate.AddDays(6) ' This gives you the end of the first week (Friday) in the previous month.

7. Do While: startDate.Month = firstDayOfPreviousMonth.Month
- Add your processing logic for each week (Monday to Friday) here.
- You can use a For Each activity with a range from startDate to endDate.

- Assign: startDate = startDate.AddDays(7) ' Move to the next week's Monday.
- Assign: endDate = startDate.AddDays(6) ' Calculate the end of the current week (Friday).

8. End While
``````

Hope it helps!!

1 Like

@wjdehdnr456

Get the First Day of the Previous Month:

Get the Last Day of the Previous Month:

Loop Through Weeks and Extract Weekdays:

1 Like

Hi @wjdehdnr456
Use the below syntaxes in assign activity:

``````currentYear= Now.Year
currentMonth= Now.Month
previousMonth= If(currentMonth > 1, currentMonth - 1, 12)
previousYear= If(currentMonth > 1, currentYear, currentYear - 1)
``````

Note: `currentYear`, `currentMonth`, `previousMonth`, `previousYear` is of DataType System.Int32
=> Use the below code in Invoke Code activity:

``````Dim firstDayOfMonth As New DateTime(previousYear, previousMonth, 1)
Dim lastDayOfPreviousMonth As DateTime = firstDayOfMonth.AddDays(-1)

Dim firstMonday As DateTime = firstDayOfMonth.AddDays(-CInt(firstDayOfMonth.DayOfWeek))
Dim lastMonday As DateTime = lastDayOfPreviousMonth.AddDays(-CInt(lastDayOfPreviousMonth.DayOfWeek))

Dim currentMonday As DateTime = firstMonday
weeklyRanges = New List(Of Tuple(Of DateTime, DateTime))

While currentMonday <= lastMonday
Dim currentFriday As DateTime = currentMonday.AddDays(4)
End While

weeklyRanges.ToArray()
``````

Below are invoked arguments:

=> Use For Each loop to iterate through arr_week and it is of DataType System.Collections.Generic.List(System.Tuple(System.DateTime,System.DateTime))
=> Use the below syntax in write line:

``````"Range: " & weeklyRange.Item1.ToString("yyyy-MM-dd") & " to " & weeklyRange.Item2.ToString("yyyy-MM-dd")
``````

Sequence.xaml (10.2 KB)

Hope it helps!!
Regards

1 Like