3

My problem is the following:

I have a start day. For example: 30 - Oct - 2010

And I know how many business days I need to count back. For example: 250 business days.

Now I want to be able to calculate the start date.

Start date = (30 - Oct - 2010) - (250 Business Days) - (Amount of weekends during those 250 days).

But I don't know how to calculate the "amount of weekends".

Could anyone help me?

Thanks in advance.

Jonathan

user84112
  • 119
  • This question vaguely remembers similar things I did very long time ago. I think I used Julian dates and, knowing a reference date, use modulo(7) for deciding if Monday, Tuesday,... May be you could have a look at http://5dspace-time.org/Calendar/Algorithm.html – Claude Leibovici Oct 30 '13 at 14:10

1 Answers1

2

You can do it this way in Matlab:

% Data:
count_back = 250;
start_day = datenum('31-Oct-2013');

% Computations:
start_weekday = mod(start_day+4,7);
aux = cumsum(mod(start_weekday-1:-1:start_weekday-max(2*count_back,3),7) < 5);
days_back = find(aux==count_back, 1);
solution_date = datestr(start_day - days_back)

Comments:

  • In the first line of computations, a 0 in start_weekday indicates that start_day is Mon; 1 indicates that it is Thu etc. The +4 is needed because the reference date of the datenum function is 01-Jan-0000.
  • The comparison <5 in the second line gives 1 for business days and 0 for weekends. The function cumsum gives the cumulative sum from yesterday backwards, reaching until max(2*count_back,3) days into the past. The number max(2*count_back,3) is always enough (it could be adjusted more finely).
  • The 1 in the third line is needed because we want the first day into the past that gives the desired count_back. If that day is Mon, the previous Sun and Sat would also have the desired count_back, but we don't want them.
Luis Mendo
  • 1,834