Calculating the date for the day of the week

During a business day (1-7), how can I calculate what is the last date of the day of the week?

Example: Today is Wednesday , 2008/11/12 and I want to know what happened last Friday .

+1


source to share


2 answers


If today

is the current day of the week, you can use something like:

days_since_friday = (((today - 1) + 7) - (6 - 1)) % 7

      

This assumes that Friday is represented by the day of the 6th of the week (that is, 1 represents Sunday). Then subtract days_since_friday

from the current date and you get the end date of the last week.

The above expression is a little more complicated than it should be. If your day of the week started at 0 on Sunday, it is simplified:



days_since_friday = ((today + 7) - 5) % 7

      

or

days_since_friday = (today + 2) % 7

      

+1


source


Generally? See Calendar Calculations .



In this narrower case during the previous week? Find the difference in days (Friday = 5, Wednesday = 3). Find the difference in weeks (last week = -7 days). When you've found the offset in days + weeks, apply that offset to the calendar date.

+4


source