我如何找到下一个工作日:MATLAB

时间:2017-02-06 03:03:54

标签: matlab date

鉴于日期= 20170203(yyyymmdd),如何找到下一个工作日,即本案例中的20170206?

1 个答案:

答案 0 :(得分:4)

date = datenum('20170203','yyyymmdd');
NBD = nextBusinessDay(date)

   NBD =

   06-Feb-2017

date = datenum('20170203','yyyymmdd');
NBD = nextBusinessDay(date,1:5) % Sunday to Thursday, as in e.g. Israel.

   NBD =

   05-Feb-2017

甚至

date = datenum('20170203','yyyymmdd');
NBD = nextBusinessDay(date,[3 5 6]) % random business days

NBD =

07-Feb-2017

函数nextBusinessDay是:

function nbd = nextBusinessDay(serTime,workdays)
w = weekday(serTime);
if exist('workdays','var')
    nwds = workdays(workdays > w);
    if isempty(nwds)
        nbd = datestr(floor(serTime + mod(7,w) + workdays(1)));
    else
        nbd = datestr(floor(serTime) + nwds(1) - w);
    end
else
    if w < 6
        nbd = datestr(floor(serTime+1));
    else
        nbd = datestr(floor(serTime + 3 - mod(w,6)));
    end
end