lweekdate - (Not recommended; use dateshift) Date of last occurrence of
weekday in month - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/lweekdate.html)) ([raw](?raw))
(Not recommended; use dateshift
) Date of last occurrence of weekday in month
Syntax
Description
[LastDate](#bu%5Fszd7-1-LastDate) = lweekdate([Weekday](#bu%5Fszd7-1-Weekday),[Year](#bu%5Fszd7-1-Year),[Month](#bu%5Fszd7-1-Month),[NextDay](#bu%5Fszd7-1-NextDay),[outputType](#bu%5Fszd7-1-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 forNextDay
requires that the week also includes the specified subsequent weekday. Setting outputType
to"datetime"
is recommended.
[LastDate](#bu%5Fszd7-1-LastDate) = lweekdate([Weekday](#bu%5Fszd7-1-Weekday),[Year](#bu%5Fszd7-1-Year),[Month](#bu%5Fszd7-1-Month))
returns the date as a serial date number. This syntax is equivalent tolweekdate(Weekday,Year,Month,0,"datenum")
.
Examples
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)
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 = 1×3 datetime 27-May-2019 25-May-2020 31-May-2021
Input Arguments
Weekday number, specified as a scalar or vector of integers between1
and 7
.
1
— Sunday2
— Monday3
— Tuesday4
— Wednesday5
— Thursday6
— Friday7
— Saturday
If you specify a vector for Weekday
and forYear
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 forWeekday
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 between1
and 12
.
If you specify a vector for Month
and forWeekday
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
and7
. A value of 0
indicates that the week only needs to include Weekday
. Values1
through 7
are the same as forWeekday.
If you specify a vector for NextDay
and forWeekday
, Year
, orMonth
, 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 returnsLastDate
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
Date for last occurrence in month, returned as a scalar or vector of typedouble
or datetime
. The type depends on the value of outputType.
Version History
Introduced before R2006a
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 adatetime
value to the end of the month usingdateshift
. 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 adatetime
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
Previously, lweekdate
required Financial Toolbox™.