Main Content

lweekdate

Date of last occurrence of weekday in month

Description

example

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

example

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

Examples

collapse all

Determine the last Monday in June 2021.

LastDate = lweekdate(2,2021,6,0,"datetime")
LastDate = datetime
   28-Jun-2021

Determine the last Monday in a week that also contains a Friday in June 2001.

LastDate = lweekdate(2,2021,6,6,"datetime")
LastDate = datetime
   21-Jun-2021

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

LastDate = lweekdate(2,2021,6)
LastDate = 738335

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

LastDate = datetime(LastDate,"ConvertFrom","datenum")
LastDate = datetime
   28-Jun-2021

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

yrs = 2019:2021;
LastDate = lweekdate(2,yrs,5,0,"datetime")
LastDate = 1x3 datetime
   27-May-2019   25-May-2020   31-May-2021

Input Arguments

collapse all

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 Year or Month, then all vectors must be the same length. The output LastDate 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 Weekday or Month, then all vectors must be the same length. The output LastDate 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 Weekday or Year, then all vectors must be the same length. The output LastDate 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 Weekday, Year, or Month, then all vectors must be the same length. The output LastDate is a vector of the same length.

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

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

Output Arguments

collapse all

Date for last occurrence in 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