nweekdate

Date of the n-th occurrence of a weekday in a given month.

Category: Language Fundamentals > Data Types

Syntax

d = nweekdate(n, weekday, year, month)

Returns the serial date number of the n-th weekday of the month.

Description

The function is useful for holidays defined by rule. Thanksgiving in the United States is the fourth Thursday of November, so nweekdate(4, 5, 2024, 11) finds it. Pass the result to datestr or datetime(d, 'ConvertFrom', 'datenum') to display it.

Input arguments

n
Occurrence number, 1 to 5.
weekday
1 for Sunday through 7 for Saturday.
year, month
Calendar year and month number (1 to 12).

Output arguments

d
Serial date number, or 0 if the month has no such occurrence (for example a fifth Tuesday that does not exist).

Example

d = nweekdate(4, 5, 2024, 11)
datestr(d)
Output
d = 739584
ans = '28-Nov-2024'

The fourth Thursday of November 2024.

See also

lweekdate weekday datenum datestr eomday