Obtain last Friday's date based on today's date

Hello all,

I need to determine a range of dates, in which the From date = last Friday’s date (mm/dd/yyyy) based on today. Any suggestions on how to accomplish this?

Hello rjackson,

Welcome to our Community!

I would use a loop with a counter that goes backwards.

Loop 1:
If Now.AddDays(-1).DayOfTheWeek.ToString = “Friday”

LastFriday = Now.AddDays(-1).ToString(“MM/dd/yyyy”)

Break

Loop 2;

If Now.AddDays(-2).DayOfTheWeek.ToString = “Friday”

LastFriday = Now.AddDays(-2).ToString(“MM/dd/yyyy”)

Loop 3;

If Now.AddDays(-3).DayOfTheWeek.ToString = “Friday”

LastFriday = Now.AddDays(-3).ToString(“MM/dd/yyyy”)

And on an on until you get a Friday.

Was it clear?

DId this help you?

Kind regards,
Daniel

Hi
welcome to uipath community
this is really interesting and i have tried this one time either
so the steps would be like this
–lets take a string variable
str_currentdate = Now.DayofWeek.ToString

–now use a while loop like this
NOT str_currendate.ToString.Equals(“Friday”)
and inside the loop use
–a assign activity like this
str_currentdate = now.AddDays(-1).Dayofweek.ToString
Final_Date = Now.AddDays(-1).ToString
–so next to this while loop use a write liine activity and mention like this
Final_Date.ToString
so the value will be last friday date

Cheers @rjackson

1 Like

Hi,

The following expression might help you. This needs no loop.

lastFridayDate = Today.AddDays(7*Cint(Today.DayOfWeek<=DayOfWeek.Friday)+(DayOfWeek.Friday-Today.DayOfWeek))

Regards,

6 Likes

Hi

This does not work - as the current date is always used for the loop. My suggestion is:

–lets take a string variable
str_currentdate = Now.DayofWeek.ToString
-also take current date as date in assign
DateTemp = Now

–now use a while loop like this
NOT str_currendate.ToString.Equals(“Friday”)
and inside the loop use
–a assign activity like this
str_currentdate = DateTemp.AddDays(-1).Dayofweek.ToString
Final_Date = DateTemp.AddDays(-1).ToString
–so next to this while loop use a write liine activity and mention like this
Final_Date.ToString
so the value will be last friday date

Let me know if you agree
Regards

Hello,

This solution is giving me an infinite loop. Could you please a xaml file with an example?

Thank you

Hi Yoichi,
read this and found this interesting can you please explain what is the logic behind this.

Thanks in Advance !

Hello All,

I have done this using while loop. Please see below screenshot.

Not sure how nobody noticed, but you’re missing a minus sign (in front of the 7, to go back a week).

lastFridayDate = Today.AddDays(-7*Cint(Today.DayOfWeek<=DayOfWeek.Friday)+(DayOfWeek.Friday-Today.DayOfWeek))

Explanation: This adjusts the date to the current Friday
(DayOfWeek.Friday-Today.DayOfWeek)

Since Saturday is day 6 in the week (Sunday is day 0), there is a special condition not subtract an extra week, but just adjust for the day of the week. This condition is converted into a 0 or 1 to be multiplied by 7.

Cint(Today.DayOfWeek<=DayOfWeek.Friday)

Maybe on Saturday you actually want to go back 8 days (to the previous week). In that case you can just remove the condition and just subtract 7 days (after adjusting to the current Friday)

lastFridayDate = Today.AddDays((DayOfWeek.Friday-Today.DayOfWeek)-7)

If you wanted Sunday to get treated the same and go back 9 days, it’s a bit trickier, since it’s technically a new week. You need to add a different condition back in. Sometimes using the MOD function to shift the days is also helpful.

As the above expression returns -1 or 0, it’s unnecessary to add minus in front of 7

image

Regards,