How to find nearest day(may vary like Sunday, Monday) of a date(variable)

I want to find the nearest day(variable) for a date(variable).
For eg, For 22 Aug, nearest Sunday is 21 Aug and for 25 Aug, nearest Sunday is 28th Aug.
Here both, the date and the nearest day to be found are dynamic.
I have created a Workflow, with variable dates and variable day of the week. But could not get the desired results.
NearestDay.zip (12.0 KB)
Request you to please help.

Hi,

Can you try the following expression?

image

targetDate.AddDays({0,-1,-2,-3,3,2,1}(CInt(targetDate.DayOfWeek)))

Regards,

1 Like

Thanks for your reply.
But your code, seems to find only the nearest Sunday. I already achieved it using Previous Sunday:
InputDate.AddDays(1-Weekday(InputDate)).ToString(“dd/MM/yyyy")
Next Sunday:
InputDate.AddDays(8-Weekday(InputDate)).ToString(“dd/MM/yyyy”)

My requirement is that the Nearest Day to be found is also dynamic. (It can be Sunday, Monday, Tuesday, Wed, Thursday, Friday, Sat). I can use your code, by adding 7 assign statements like yours. But can there be just one line of code which can solve my requirement.

Hi @SumitTyagi1810

Try this

image

Refer the xaml

NearestDay.xaml (5.3 KB)

Hi,

How about the following?

targetDate = New DateTime(2022,8,22)
arr= {-1,-2,-3,3,2,1,0,-1,-2,-3,3,2,1}
wd = CInt(DayOfWeek.Sunday)

then

targetDate.AddDays(arr.skip(6-wd)(CInt(targetDate.DayOfWeek)))

Sequence.xaml (6.5 KB)

Regards,

1 Like

Hello @SumitTyagi1810
Try this mehod using modify date activities and if condition
NearestSunday.zip (68.1 KB)

Happy Automation

Regards
Gokul Jai

Thanks. It worked.

Made minor changes to make it suitable for loops. Instead of using wd =CInt(DayOfWeek.Sunday), as Day of Week is constant here and was not accepetng dynamic value, I made a dictionary for each weekday and assigned corresponding value of Day of week, and thus used it for skip array evaluations.

1 Like

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