From: Luna Moon on
It's like this:

Today is 6/22/2010, last year's today is 6/22/2009, and today 2 years
agoo is 6/22/2008.

This is simple to do in Matlab.

How about what about business days and weekend...?

I am looking for something like today in last year...

but if that was a weekend or holiday,

then I would like the date before that and the date after that.

How to do this in Matlab?

Thanks
From: Walter Roberson on
Luna Moon wrote:
> It's like this:
>
> Today is 6/22/2010, last year's today is 6/22/2009, and today 2 years
> agoo is 6/22/2008.
>
> This is simple to do in Matlab.
>
> How about what about business days and weekend...?
>
> I am looking for something like today in last year...
>
> but if that was a weekend or holiday,
>
> then I would like the date before that and the date after that.
>
> How to do this in Matlab?

Not too easily -- Holidays can be at the town level, and definitely can be at
the State level in the USA. In Canada, there is St Jean Baptiste Day only in
Quebec but Quebec did not adopt the day in February called various things such
as "Family Day" that some of the other provinces adopted. Federally-mandated
holidays in Canada are different than those in the USA, and bank holidays have
different rules yet.

You would have to start by defining what a holiday is for your purpose. If you
include Easter or Passover or Dwali or any of the various movable feasts, then
you may have to make non-trivial calculations about lunar cycles and
solstices. You might even hit the problem that at least one major holiday in
the middle east is defined in terms of seeing two of three particular stars
rising, and thus because of latitude differences can theoretically occur on
different days in different areas -- and because of weather differences can
get postponed....

Then there are the wonders of Leap Year, which has different definitions in
different calendars, including certain Muslim calendars which ban "Leap Year"
and instead add "intercalculary months".

Oh yes, the weekend begins at Sunset on Friday in the Jewish calendar, so if
you want to know about weekends then you better know the latitude and
longitude to calculate the time of Sunset...
From: Doug Weathers on
Hi Luna,

You could use the functions WEEKDAY and HOLIDAYS to figure this out.

Example (only very lightly tested for correctness, so beware):

tnum = today;

td = day(tnum);
tm = month(tnum);
ty = year(tnum);
ly = ty - 1;

lynum = datenum(ly, tm, td);

isholiday = @(day) not(isempty(holidays(day,day)));
isweekend = @(day) ismember(weekday(day), [1 7]);

while isholiday(lynum) || isweekend(lynum)
lynum = lynum - 1;
end

fprintf('The business day closest to today in %d was: %s\n', ly, datestr(lynum))



Luna Moon <lunamoonmoon(a)gmail.com> wrote in message <9e358331-2765-4d86-a871-a93eb8bdc3b5(a)b5g2000vbl.googlegroups.com>...
> It's like this:
>
> Today is 6/22/2010, last year's today is 6/22/2009, and today 2 years
> agoo is 6/22/2008.
>
> This is simple to do in Matlab.
>
> How about what about business days and weekend...?
>
> I am looking for something like today in last year...
>
> but if that was a weekend or holiday,
>
> then I would like the date before that and the date after that.
>
> How to do this in Matlab?
>
> Thanks
From: Walter Roberson on
Doug Weathers wrote:

> You could use the functions WEEKDAY and HOLIDAYS to figure this out.

That appears to require the Financial toolbox.

I note the default holidays are those of the New York Stock Exchange.
From: Doug Weathers on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hvtunv$eor$1(a)canopus.cc.umanitoba.ca>...
> Doug Weathers wrote:
>
> > You could use the functions WEEKDAY and HOLIDAYS to figure this out.
>
> That appears to require the Financial toolbox.

Good catch! I hadn't noticed that. I was just poking at my school copy of MATLAB, wondering if there were any useful date functions built in. HELP WEEKDAY got me started.

The function WEEKDAY appears to reduce to the formula

weekday = mod(datenum-2, 7) + 1;

while HOLIDAYS merely contains a long list of date numbers for New York Stock Exchange holidays from 1950 to 2050. It returns the holidays between the given datenums.

To reproduce it, you could type in a list of your particular holidays from a calendar into a vector, then check to see if your datenum is a member of that set with ISMEMBER.