datetime

Methods

Method Reference: datetime.week

datetime: W = week (T)
datetime: W = week (T, weekType)

Week component of a datetime array.

W = week (T) returns the week-of-year number for each element of the input datetime array T. The output W is a double array containing integer values in the range [1, 54] and it has the same size as T. Weeks are counted from Sunday to Saturday, and the week containing January 1 is week 1. Not-A-Time (NaT) values in T are returned as NaN in the output array.

W = week (T, weekType) returns the week number for each element of the input datetime array T as specified by weekType, which may have any of the following options:

  • 'weekofyear' (default) returns the week of the year in a numeric array, in the range [1, 54].
  • 'weekofmonth' returns the week of the month in a numeric array, in the range [1, 6].

Source Code: datetime

week returns the week of the year, counted Sunday to Saturday with the week that contains January 1 as week 1. (This is not the ISO 8601 rule.)

 d = datetime (2024, 1, 1) : caldays (7) : datetime (2024, 2, 12)
d =
  1x7 datetime array

    01-Jan-2024    08-Jan-2024    15-Jan-2024    22-Jan-2024    29-Jan-2024    05-Feb-2024    12-Feb-2024
 week (d)
ans =

   1   2   3   4   5   6   7

Pass 'weekofmonth' for the week within the month instead of the year.

 week (datetime (2024, 3, 15), 'weekofmonth')
ans = 3