nweekdate
Date of specific occurrence of weekday in month
Syntax
Description
returns the date for the Date
= nweekdate(n
,Weekday
,Year
,Month
,NextDay
,outputType
)n
th 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
n
th occurrence. A nonzero value for
NextDay
requires that the week also includes the specified
subsequent weekday. Setting outputType
to
"datetime"
is recommended.
Examples
Single Date
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
Multiple Dates
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
n
— n
th occurrence of weekday in month
scalar | vector
n
th 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
— Weekday number
scalar | vector
Weekday number, specified as a scalar or vector of integers between
1
and 7
.
1
— Sunday2
— Monday3
— Tuesday4
— Wednesday5
— Thursday6
— Friday7
— 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
— Year
scalar | vector
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
— Month number
scalar | vector
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.
NextDay
— Subsequent weekday in week
0
(default) | scalar | vector
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.
outputType
— Output data type
"datenum"
(default) | "datetime"
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
Date
— Date of specified occurrence in month
double
| datetime
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 R2006aR2022a: Returning serial date numbers not recommended
Not recommended starting in R2022a
Returning serial date numbers from nweekdate
is not
recommended. To return datetime
values instead, call
nweekdate
with the "datetime"
argument.
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.
For example, return the first Tuesday of October, 2021 as a
datetime
value.
firstTuesday = nweekdate(1,3,2021,10,0,"datetime")
R2022a: Moved to MATLAB from Financial Toolbox
Previously, nweekdate
required Financial Toolbox™.
Apri esempio
Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)