How can I count the number of working days between 2 dates?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Plus de réponses (1)
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.
0 commentaires
Voir également
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!