weeknum - (Not recommended; use week) Week in year - MATLAB (original) (raw)
Main Content
(Not recommended; use week
) Week in year
Syntax
Description
[N](#bu%5Frnjc-1-N) = weeknum([D](#bu%5Frnjc-1-D))
returns the week in year. The weeknum
function considers the week containing January 1 to be the first week of the year.
[N](#bu%5Frnjc-1-N) = weeknum(___,[W](#bu%5Frnjc-1-W),[E](#bu%5Frnjc-1-E))
returns the week in year using the optional input arguments forW
and E
. Theweeknum
function considers the week containing January 1 to be the first week of the year.
Examples
Determine the week of the year using a serial date number.
Determine the week of the year using a string.
N = weeknum("19-Dec-1994")
Determine the week of the year using a datetime
array.
N = weeknum(datetime("19-Dec-1994","Locale","en_US"))
The first week of the year must have at least four days in it. For example, January 8, 2004, was a Thursday. The European standard is used because the first week of the year is the first week longer than three days.
weeknum("08-jan-2004",1,1)
You can also use weeknum
with datenum.
weeknum(datenum("01-Jan-2004"):datenum("08-Jan-2004"))
ans = 1×8
1 1 1 2 2 2 2 2
The default start day of the week is Sunday. Every day after, and including the first Sunday of the year (04-Jan-2004), returns 2
denoting the second week. In this case, the first of week of the year started before January 1, 2004. You can also use weeknum
with datenum and specify a D
value of 5
to indicate that the weeks start on Thursday.
weeknum(datenum("01-Jan-2004"):datenum("08-Jan-2004"),5)
ans = 1×8
1 1 1 1 1 1 1 2
The first week of the year that has four or more days, based on the specified start day, is considered week one (even if this is not the first week in the calendar). Any day falling in (or before) this week is given a week number of 1.
Input Arguments
Date to determine week in year, specified as a serial date number, date strings, or datetime
array.
Serial date numbers can be a matrix. Date strings can be specified as a string vector or a one-dimensional cell array of character vectors. All the date strings must have the same format.
Data Types: double
| string
| char
| datetime
Day a week begins, specified as an integer or a vector of integers from1
through 7
.
1
— Sunday (default)2
— Monday3
— Tuesday4
— Wednesday5
— Thursday6
— Friday7
— Saturday
The weeknum
function considers the week containing January 1 to be the first week of the year.
Data Types: double
Flag indicates if week of year display is European standard, specified as1
(to use the European standard) or0
(not to use the European standard).
The European standard considers first week of year to be first week longer than three days, offset by the given week’s start day.
Data Types: logical
Output Arguments
Week number of the year, given D, returned as a numeric value, given D
, a serial date number, date string, or datetime
array. If D
is a string vector or a one-dimensional cell array of character vectors, thenweeknum
returns a column vector of_M_ week numbers, where M is the number of elements in D
.
If the optional input arguments W andE are defined, the week of the year is in the European standard.
Version History
Introduced before R2006a
There are no plans to remove weeknum
. However, the week function is recommended instead because it accepts datetime
values as inputs. Thedatetime
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 week in the year, call week
with the"weekofyear"
argument. If dt
is adatetime
value, then this week
syntax returns the week of the year for dt
.
dt = datetime("today") weekNumber = week(dt,"weekofyear")
Previously, weeknum
required Financial Toolbox™.