Main Content

lweekdate

(Not recommended; use dateshift) Date of last occurrence of weekday in month

lweekdate is not recommended because it returns a serial date number. To return the last occurrence of a weekday as a datetime value, use the dateshift function. If you do use lweekdate, consider setting the outputType option to "datetime". For more information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.

Description

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").

example

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

R2022a: Not recommended

There are no plans to remove lweekdate. However, the dateshift function is recommended instead because it returns datetime values, not serial date numbers. The datetime data type provides flexible date and time formats, storage out to nanosecond precision, and properties to account for time zones and daylight saving time.

To return the last occurrence of a weekday in a month, start by specifying the last day of the month as a datetime value. You can either create it directly using the datetime function or shift a datetime value to the end of the month using dateshift. Then use dateshift to shift the end of the month to the last occurrence of the specified weekday.

For example, return the last Tuesday of October, 2021 as a datetime value.

october = datetime(2021,10,1);
endOfOctober = dateshift(october,"end","month");
lastTuesday = dateshift(endOfOctober,"dayofweek","Tuesday","previous")
lastTuesday = datetime
   26-Oct-2021

If you do plan to use lweekdate, consider returning its output as a datetime value instead of a serial date number.

lastTuesday = lweekdate(3,2021,10,0,"datetime")
lastTuesday = datetime
   26-Oct-2021