weeknum
(Not recommended; use week
) Week in year
weeknum
is not recommended. Use the week
function instead because it accepts datetime
values as inputs. For
more information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.
Description
Examples
Determine the Week of the Year
Determine the week of the year using a serial date number.
N = weeknum(728647)
N = 52
Determine the week of the year using a string.
N = weeknum("19-Dec-1994")
N = 52
Determine the week of the year using a datetime
array.
N = weeknum(datetime("19-Dec-1994","Locale","en_US"))
N = 52
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)
ans = 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
D
— Date to determine week in year
serial date number | date string | datetime
array
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
W
— Day a week begins
1
(default) | integer with value 1
through
7
| vector of integers with values 1
through
7
Day a week begins, specified as an integer or a vector of integers from
1
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
E
— Flag indicates if week of year display is European standard
0
(default) | numeric with values 1
or 0
Flag indicates if week of year display is European standard, specified as
1
(to use the European standard) or
0
(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
N
— Week number of the year, given D
numeric | column vector
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, then
weeknum
returns a column vector of
M week numbers, where M is the
number of elements in D
.
If the optional input arguments W
and
E
are defined, the week of the year is in the
European standard.
Version History
Introduced before R2006aR2022a: Not recommended
There are no plans to remove weeknum
. However, the week
function is recommended
instead because it accepts datetime
values as inputs. 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 week in the year, call week
with the
"weekofyear"
argument. If dt
is a
datetime
value, then this week
syntax
returns the week of the year for dt
.
dt = datetime("today") weekNumber = week(dt,"weekofyear")
R2022a: Moved to MATLAB from Financial Toolbox
Previously, weeknum
required Financial Toolbox™.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)