Documentation

lweekdate

Date of last occurrence of weekday in month

Syntax

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

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.

outputType

(Optional) A character vector specified as either 'datenum' or 'datetime'. The output Date will be in serial date format if 'datenum' is specified or datetime format if 'datetime' is specified. By default, the output will be in serial date format.

 

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, outputType) 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.

The type of the output depends on the input outputType. If this variable is 'datenum', LasteDate will be a serial date number. If outputType is 'datetime', then LastDate will be a datetime array. By default, outputType is set to 'datenum'.

Use the function datestr to convert serial date numbers to formatted date character vectors.

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 returned as a datetime array:

LastDate = lweekdate(2, 2001, 6,[],'datetime')
LastDate = 

   25-Jun-2001

Example 3. 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 4. 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

Related Examples

Introduced before R2006a

Was this topic helpful?