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.
Hi @wjdehdnr456
Use the date manipulation for this.
1. Assign: currentDate = DateTime.Now
2. Assign: firstDayOfPreviousMonth = currentDate.AddMonths(-1).AddDays(-currentDate.Day + 1)
3. Assign: weekdayOfFirstDay = firstDayOfPreviousMonth.DayOfWeek
4. Assign: daysToMonday = (weekdayOfFirstDay - DayOfWeek.Monday + 7) % 7
5. Assign: startDate = firstDayOfPreviousMonth.AddDays(-daysToMonday)
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!!
Get the First Day of the Previous Month:
DateTime.Now.AddDays(-DateTime.Now.Day).AddDays(-DateTime.Now.Day + 1).ToString(“MM/dd/yyyy”)
Get the Last Day of the Previous Month:
DateTime.Now.AddDays(-DateTime.Now.Day).ToString(“MM/dd/yyyy”)
Loop Through Weeks and Extract Weekdays:
new List(Of DateTime)(Enumerable.Range(0, 31).Select(Function(i) DateTime.Now.AddDays(-DateTime.Now.Day).AddDays(i)).Where(Function(d) d.Month = DateTime.Now.AddMonths(-1).Month AndAlso d.DayOfWeek >= DayOfWeek.Monday AndAlso d.DayOfWeek <= DayOfWeek.Friday))
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)
weeklyRanges.Add(Tuple.Create(currentMonday, currentFriday))
currentMonday = currentMonday.AddDays(7)
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