Borrar filtros
Borrar filtros

How to find out number of Fridays in a month?

3 visualizaciones (últimos 30 días)
Kevin
Kevin el 11 de Ag. de 2022
Respondida: Cris LaPierre el 11 de Ag. de 2022
I guess I can use lweekdate and nweekdate. Just wondering if there is a faster way to do this.

Respuestas (2)

Stephen23
Stephen23 el 11 de Ag. de 2022
Here is a really simple approach using CALENDAR():
M = calendar(2022,8);
N = nnz(M(:,6))
N = 4
Otherwise you could try using a DATETIME array:
D = datetime(2022,8,1);
V = D:D+calmonths(1)-caldays(1);
N = nnz(weekday(V)==6)
N = 4

Cris LaPierre
Cris LaPierre el 11 de Ag. de 2022
Not sure what your current code is, but what about something like this using weekday?
year = 2022;
month = 4;
dt = datetime(year,month,1):caldays(1):dateshift(datetime(year,month,1),'end',"month");
dys = weekday(dt)
dys = 1×30
6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7
nFr = sum(dys==6)
nFr = 5

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by