How to add week range based on date

Hi All,

I need to add week range(Mon-sat) based on date provided
Scenario1:-
if date is 30/01/2024 then 29/01/2024 to 03/01/2024
if date is 03/02/2024 or 04/02/2024 then 05/02/2024 to 10/02/20324

please advise

Hi @Mopur_Kishore1

- Assign -> inputDate As DateTime = DateTime.ParseExact("30/01/2024", "dd/MM/yyyy", CultureInfo.InvariantCulture)

- Assign -> WeekRange As String = (inputDate.AddDays((System.DayOfWeek.Monday - inputDate.DayOfWeek - 7)).ToString("dd/MM/yyyy") & " to " & inputDate.AddDays((System.DayOfWeek.Saturday - inputDate.DayOfWeek)).ToString("dd/MM/yyyy"))

Check the below workflow for better understanding,

Hope it helps!!

1 Like

@Mopur_Kishore1

Assign DataTable Variable:
dtWeekRanges = New DataTable()
dtWeekRanges.Columns.Add("Start Date", GetType(DateTime))
dtWeekRanges.Columns.Add("End Date", GetType(DateTime))

For i As Integer = 0 To 6
    Dim currentDate As DateTime = DateTime.ParseExact("30/01/2024", "dd/MM/yyyy", CultureInfo.InvariantCulture).AddDays(-i)
    Dim startDate As DateTime = currentDate.AddDays(DayOfWeek.Monday - currentDate.DayOfWeek)
    Dim endDate As DateTime = currentDate.AddDays(DayOfWeek.Saturday - currentDate.DayOfWeek)
    dtWeekRanges.Rows.Add(startDate, endDate)
Next

Use Invoke code activity

Is it working for you @Mopur_Kishore1

If yes mark my post as solution to close the loop, else if you have any clarification needed let me know, Happy to help…

Happy Automation!!

sorry i have made modification in the query please look it again
Here is the updated one

if date is 30/01/2024 then bot should return week range
29/01/2024 to 03/01/2024

If date is 03/02/2024 or 04/02/2024(sat or sun) then
week range should be 05/02/2024 to 10/02/20324

Hi,

Can you try the following sample?

targetDateStr = "04/02/2024"

targetDate = DateTime.ParseExact(targetDateStr,"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture)

Then

fromDateStr =targetDate.AddDays(if(targetDate.DayOfWeek<6,1-CInt(targetDate.DayOfWeek),2)).ToString("dd/MM/yyyy")

toDateStr = targetDate.AddDays(if(targetDate.DayOfWeek<6,6-CInt(targetDate.DayOfWeek),7)).ToString("dd/MM/yyyy")

Sample
Sample20240129-1a.zip (2.6 KB)

Regards,

1 Like

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