datetime

Methods

Method Reference: datetime.dateshift

datetime: R = dateshift (A, 'start', unit)
datetime: R = dateshift (A, 'end', unit)
datetime: R = dateshift (…, rule)
datetime: R = dateshift (A, 'dayofweek', dow)
datetime: R = dateshift (A, 'dayofweek', dow, rule)

Shift datetime values to calendar boundaries.

R = dateshift (A, 'start', unit) returns a datetime array in which each element of A is moved back to the start of the calendar unit that contains it, with the finer components set to zero. unit is 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', or 'year'. A week starts on Sunday.

R = dateshift (A, 'end', unit) moves each element to the end of its unit: the start of the next second, minute, hour, or day, and the last day (at midnight) of the week, month, quarter, or year.

R = dateshift (…, rule) first shifts each element by rule whole units. rule is 'current' (the default), 'next', 'previous', 'nearest', or an integer number of units.

R = dateshift (A, 'dayofweek', dow) moves each element to the next date, on or after it, whose day of the week is dow (a number from 1 for Sunday to 7 for Saturday, or a day name), keeping the time of day. A trailing rule of 'previous', 'nearest', 'current' (the day within the current week), or an integer occurrence selects a different date.

Not-A-Time and infinite elements are returned unchanged.

Source Code: datetime

dateshift snaps a datetime to a calendar boundary — the start or end of a unit such as the month.

 t = datetime (2024, 3, 15, 10, 30, 0)
t =
  datetime

   15-Mar-2024 10:30:00
 dateshift (t, 'start', 'month')
ans =
  datetime

   01-Mar-2024
 dateshift (t, 'end', 'month')
ans =
  datetime

   31-Mar-2024

The 'dayofweek' mode moves to the next occurrence of a weekday, keeping the time of day.

 t = datetime (2024, 3, 15, 10, 30, 0)
t =
  datetime

   15-Mar-2024 10:30:00
 dateshift (t, 'dayofweek', 'Monday')
ans =
  datetime

   18-Mar-2024 10:30:00