lweekdate

Date of the last occurrence of a weekday in a month.

Category: Language Fundamentals > Data Types

Syntax

d = lweekdate(weekday, year, month)

Returns the serial date number of the last given weekday of month in year.

d = lweekdate(weekday, year, month, nextDay)

Returns the last weekday that is followed by a nextDay in the same month.

Description

Rules such as "the last Friday of May" or "the last Monday of the month" are common in calendars and financial schedules. lweekdate finds them without looping over days. Its counterpart for the n-th occurrence is nweekdate.

The function belongs to the Financial Toolbox in MATLAB.

Input arguments

weekday
Day of the week as a number: 1 is Sunday, 2 Monday, ..., 7 Saturday.
year, month
Four-digit year and month number from 1 to 12.
nextDay
Optional weekday number (1 to 7) restricting which month-end dates are valid.

Output arguments

d
Serial date number (as returned by datenum). Use datestr or datetime(d, 'ConvertFrom', 'datenum') to display it.

Example

Last Friday and last Monday of May 2024

datestr(lweekdate(6, 2024, 5))
datestr(lweekdate(2, 2024, 5))
Output
ans = '31-May-2024'
ans = '27-May-2024'

See also

nweekdate datenum datestr weekday eomday