How can I count the number of working days between 2 dates?
4 次查看(过去 30 天)
显示 更早的评论
How can I count the number of working days between two dates?
Does exist a function?
eg: t1 = 10-apr-2017; t2 = 17-apr-2017; -> x = function(t1,t2) = 5
Thanks
0 个评论
采纳的回答
更多回答(1 个)
Peter Perkins
2017-4-26
If you only care about weekdays vs. weekends, try this:
>> t = datetime({'10-apr-2017' '17-apr-2017'},'Format','eee dd-MM-yyyy')
t =
1×2 datetime array
Mon 10-04-2017 Mon 17-04-2017
This gives you the number of days ...
>> caldiff(t,'days')
ans =
calendarDuration
7d
... but you don't want to count weekends. So create this sequence of days ...
>> tseq = t(1):caldays(1):t(2)
tseq =
1×8 datetime array
Columns 1 through 7
Mon 10-04-2017 Tue 11-04-2017 Wed 12-04-2017 Thu 13-04-2017 Fri 14-04-2017 Sat 15-04-2017 Sun 16-04-2017
Column 8
Mon 17-04-2017
... and count non-weekend days.
>> sum(~isweekend(tseq(1:end-1)))
ans =
5
Holidays are very localized. If you have a list of the holidays that matter to you, create them as a datetime vector, then add a call to ismember inside that sum, something like
sum(~isweekend(tseq(1:end-1)) & ismember(tseq(1:end-1),holidays))
But dpb is right, busdays is probably what you want.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!