How can I count the number of working days between 2 dates?

3 vues (au cours des 30 derniers jours)
eagle
eagle le 26 Avr 2017
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

Réponse acceptée

dpb
dpb le 26 Avr 2017
See <busdays> but need Financial Toolbox

Plus de réponses (1)

Peter Perkins
Peter Perkins le 26 Avr 2017
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.

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by