How to get previous business day (need to consider weekends)

Hi!

Can someone help me get the previous business day in a calendar? Please note that I need to consider if the previous day is a weekend. So if today is Monday, what I need to get is the Friday last week. See sample below.

image

Thanks!

DateTime prevDay = date.AddDays(-1)
while (prevDay.DayOfWeek == DayOfWeek.Saturday || prevDay.DayOfWeek == DayOfWeek.Sunday)
    {
        prevDay = prevDay.AddDays(-1)
    }

Regards,
Karthik Byggari

1 Like

An alternate way -

DateTime prevDay = date.AddDays(-1)

if(prevDay.DayOfWeek = DayOfWeek.Saturday)
prevDay = prevDay.AddDays(-1)


if(prevDay.DayOfWeek = DayOfWeek.Sunday)
prevDay = prevDay.AddDays(-2)

Regards,
Karthik Byggari

Thank you for the feedback. But I’m having a hard time picturing this one.

So I should use IF Activity, and in my condition is ‘if(prevDay.DayOfWeek = DayOfWeek.Saturday)’, and in my then is ‘prevDay = prevDay.AddDays(-1)’. In my else is another if statement with the condition, ‘if(prevDay.DayOfWeek = DayOfWeek.Sunday)’ and in my then is ‘prevDay = prevDay.AddDays(-2)’

Is my understanding correct?

Yes.