lweekdate

Date of last occurrence of weekday in month

Syntax

LastDate = lweekdate(Weekday, Year, Month, NextDay)

Arguments

Weekday

Weekday whose date you seek. Enter as an integer from 1 through 7:

1

Sunday

2

Monday

3

Tuesday

4

Wednesday

5

Thursday

6

Friday

7

Saturday

Year

Year. Enter as a four-digit integer.

Month

Month. Enter as an integer from 1 through 12.

NextDay

(Optional) Weekday that must occur after Weekday in the same week. Enter as an integer from 0 through 7, where 0 = ignore (default) and 1 through 7 are the same as for Weekday.

Any input can contain multiple values, but if so, all other inputs must contain the same number of values or a single value that applies to all. For example, if Year is a 1-by-n vector of integers, then Month must be a 1-by-n vector of integers or a single integer. LastDate is then a 1-by-n vector of date numbers.

Description

LastDate = lweekdate(Weekday, Year, Month, NextDay) returns the serial date number for the last occurrence of Weekday in the given year and month and in a week that also contains NextDay.

Use the function datestr to convert serial date numbers to formatted date strings.

Examples

Example 1. To find the last Monday in June 2001

LastDate = lweekdate(2, 2001, 6); datestr(LastDate)
ans =

25-Jun-2001

Example 2. To find the last Monday in a week that also contains a Friday in June 2001

LastDate = lweekdate(2, 2001, 6, 6); datestr(LastDate)
ans =

25-Jun-2001

Example 3. To find the last Monday in May for 2001, 2002, and 2003

Year = [2001:2003];
LastDate = lweekdate(2, Year, 5)
LastDate =

           730999      731363      731727
datestr(LastDate)
ans =

28-May-2001
27-May-2002
26-May-2003
Was this topic helpful?