Get next business day without taking into account holidays and saturdays

8 次查看(过去 30 天)
chiefjia
chiefjia2021-10-17
回答: the cyclist ,2021-10-17
Dear MATLAB experts,
I'm trying to get the next business day of each date in an array without taking into account saturdays, but I can't manage to do so with the function 'busdate' from MATLAB. The aim is to get the next so named "weekday" discarding holidays for each one of the dates in the array. I would really appreciate your help, thank you in advance.
  4 个评论
chiefjia
chiefjia 2021-10-17
Dear the cyclist,
I've managed to solve this issue by using an if condition with 'weekday'. I was trying to impute the missing values of a date array of one column with the next business date of each row in another array with one column. Here my code with the solution integrated:
for i=1:length(reporting_date)
if isnat(reporting_date(i))
reporting_date(i) = busdate(position_date(i));
if isweekend(datetime(reporting_date(i))) == 1
reporting_date(i) = busdate(position_date(i), 2);
end
end
end
As an example, I computed:
busdate(20170610, 1)
ans = 20170611
20170611 is actually a sunday. The same happens with 20170609:
busdate(20170609, 1)
ans = 20170610
20170610 is a Saturday.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2021-10-17
The issue is not a problem with busdate(), but with your understanding of the input. 20170610 is being interpreted as a serial date number. (See this documentation.)
datestr(20170610) % Serial date number 20170610 is April 2, 5225 which is a Wednesday
ans = '02-Apr-5225'
busdate(20170610, 1)
ans = 20170611
Here is your intended result, by first converting from the serial date number to a datetime.
busdate(datetime(num2str(20170610),'InputFormat','yyyyMMdd')) % Converted the serial date number to a datetime
ans = datetime
12-Jun-2017
Using the canonical datetime format for all dates in MATLAB is highly encouraged.

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by