Main Content

nweekdate

Date of specific occurrence of weekday in month

Description

example

Date = nweekdate(n,Weekday,Year,Month,NextDay,outputType) returns the date for the nth occurrence of Weekday in the given year and month. Weekday is an integer from 1 (Sunday) through 7 (Saturday), and n is an integer from 1 through 5 that specifies the nth occurrence. A nonzero value for NextDay requires that the week also includes the specified subsequent weekday. Setting outputType to "datetime" is recommended.

example

Date = nweekdate(n,Weekday,Year,Month) returns the date as a serial date number. This syntax is equivalent to nweekdate(n,Weekday,Year,Month,0,"datenum").

Examples

collapse all

Determine the first Friday in June 2021.

Date = nweekdate(1,6,2021,6,0,"datetime")
Date = datetime
   04-Jun-2021

Find the first Friday in a week that also contains a Monday in June 2021.

Date = nweekdate(1,6,2021,6,2,"datetime")
Date = datetime
   11-Jun-2021

If you specify only n, the weekday, year, and month, nweekdate returns a serial date number.

Date = nweekdate(1,6,2021,6)
Date = 738311

However, using datetime values is recommended. To convert serial date numbers, use the datetime function.

Date = datetime(Date,"ConvertFrom","datenum")
Date = datetime
   04-Jun-2021

Determine the first Monday in May for 2019, 2020, and 2021.

yrs = 2019:2021;
Date = nweekdate(1,2,yrs,5,0,"datetime")
Date = 1x3 datetime
   06-May-2019   04-May-2020   03-May-2021

Input Arguments

collapse all

nth occurrence of the weekday in a month, specified as a scalar or vector of integers between 1 and 5.

If n is larger than the last occurrence of Weekday, then the output is either a serial date number whose value is zero, or a datetime value whose date is December 31, 1 BCE.

If you specify a vector for n and for Weekday, Year, or Month, then all vectors must be the same length. The output Date is a vector of the same length.

Weekday number, specified as a scalar or vector of integers between 1 and 7.

  • 1 — Sunday

  • 2 — Monday

  • 3 — Tuesday

  • 4 — Wednesday

  • 5 — Thursday

  • 6 — Friday

  • 7 — Saturday

If you specify a vector for Weekday and for n, Year, or Month, then all vectors must be the same length. The output Date is a vector of the same length.

Year, specified as a scalar or vector of four-digit integer values.

If you specify a vector for Year and for n, Weekday, or Month, then all vectors must be the same length. The output Date is a vector of the same length.

Month number, specified as scalar or vector of integers between 1 and 12.

If you specify a vector for Month and for n, Weekday, or Year, then all vectors must be the same length. The output Date is a vector of the same length.

Subsequent weekday in week to include with Weekday, specified as a scalar or vector of integers between 0 and 7. A value of 0 indicates that the week only needs to include Weekday. Values 1 through 7 are the same as for Weekday.

If you specify a vector for NextDay and for n, Weekday, Year, or Month, then all vectors must be the same length. The output Date is a vector of the same length.

Output data type, specified as "datenum" or "datetime".

Specifying "datetime" is recommended, which returns Date as a datetime scalar or vector. For compatibility with previous releases, the default value is "datenum", which returns Date as serial date numbers.

Output Arguments

collapse all

Date for the specified occurrence in the month, returned as a scalar or vector of type double or datetime. The type depends on the value of outputType.

Version History

Introduced before R2006a

expand all

Not recommended starting in R2022a