Main Content

# weeknum

## Syntax

``[N] = weeknum(D)``
``[N] = weeknum(___,W,E)``

## Description

example

````[N] = weeknum(D)` returns the week in year. The `weeknum` function considers the week containing January 1 to be the first week of the year.```

example

````[N] = weeknum(___,W,E)` returns the week in year using the optional input arguments for `W` and `E`. The `weeknum` function considers the week containing January 1 to be the first week of the year.```

## Examples

collapse all

Determine the week of the year using a serial date number.

`N = weeknum(728647)`
```N = 52 ```

Determine the week of the year using a character vector.

`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

collapse all

Date to determine week in year, specified as a serial date number, date character vector, or datetime array.

Serial date numbers can be a matrix. Date character vectors can be specified as a one-dimensional cell array of character vectors. All the date character vectors must have the same format.

Use the function `datestr` to convert serial date numbers to formatted date character vectors.

Data Types: `single` | `double` | `char` | `datetime`

Day a week begins, specified as an integer or a vector of integers from `1` through `7`.

• `1` — Sunday (default)

• `2` — Monday

• `3` — Tuesday

• `4` — Wednesday

• `5` — Thursday

• `6` — Friday

• `7` — Saturday

The `weeknum` function considers the week containing January 1 to be the first week of the year.

Data Types: `single` | `double`

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

collapse all

Week number of the year, given `D`, returned as a numeric value, given `D`, a serial date number, date character vector, or datetime array. If `D` is a one-dimensional cell array of character vectors, then `weeknum` returns a column vector of M week numbers, where M is the number of character vectors in `D`.

If the optional input arguments `W` and `E` are defined, the week of the year is in the European standard.

## See Also

### Topics

Introduced before R2006a

Download ebook