Documentation

nweekdate

Date of specific occurrence of weekday in month

Syntax

Date = nweekdate(n, Weekday, Year, Month, Same, outputType)

Arguments

n

Nth occurrence of the weekday in a month. Enter as integer from 1 through 5.

Weekday

Weekday whose date you seek. Enter as 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.

Same

(Optional) Weekday that must occur in the same week with Weekday. Enter as an integer from 0 through 7, where 0 = ignore (default) and 1 through 7 are 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.

 

Description

Date = nweekdate(n, Weekday, Year, Month, Same, outputType) returns the date number for the specific occurrence of the weekday in the given year and month, and in a week that also contains the weekday Same.

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

If n is larger than the last occurrence of Weekday, Date = 0.

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. Date is then a 1-by-n vector of date numbers.

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

Examples

To find the first Thursday in May 2001:

Date = nweekdate(1, 5, 2001, 5); datestr(Date)
ans =

03-May-2001

To find the first Thursday in May 2001 returned as a datatime array:

Date = nweekdate(1, 5, 2001, 5,[],'datetime')
Date = 

   03-May-2001

To find the first Thursday in a week that also contains a Wednesday in May 2001:

Date = nweekdate(2, 5, 2001, 5, 4); datestr(Date)
ans =

10-May-2001

To find the third Monday in February for 2001, 2002, and 2003:

Year = [2001:2003];
Date = nweekdate(3, 2, Year, 2)
Date =
       730901      731265      731629
datestr(Date)
ans =

19-Feb-2001
18-Feb-2002
17-Feb-2003

Related Examples

Introduced before R2006a

Was this topic helpful?