datetime

Methods

Method Reference: datetime.colon

datetime: R = colon (A, B)
datetime: R = colon (A, step, B)

Create a range of datetime values.

R = colon (A, B) is the equivalent of the syntax R = A:B and returns a row vector of datetime values starting at A and increasing in steps of one calendar day up to, and possibly including, B.

R = colon (A, step, B) is the equivalent of the syntax R = A:step:B and uses the specified step between consecutive elements. step may be:

  • a duration or a numeric scalar (a number of fixed 24-hour days), in which case successive elements advance by a fixed amount of elapsed time; for a zoned range this is aware of daylight saving time.
  • a calendarDuration, in which case successive elements advance in calendar units. Each element is computed as A + k*step for k = 0, 1, 2, …, so month and year steps clamp the day of month independently for every element (e.g. a one-month step from 31 January yields 31 January, 28 February, 31 March, …).

The default step of A:B is one calendar day (caldays (1)), which preserves the time of day across daylight saving time changes. A range whose step points away from B (for example an increasing step with A > B) is empty. A and B must be datetime scalars that are either both zoned or both unzoned, and must be finite.

Source Code: datetime

The colon operator builds a range of datetimes; the step is a duration or calendarDuration. A caldays step gives one row per calendar day.

 datetime (2024, 1, 1) : caldays (1) : datetime (2024, 1, 4)
ans =
  1x4 datetime array

    01-Jan-2024    02-Jan-2024    03-Jan-2024    04-Jan-2024

Use a time-based step for a finer range — here every six hours.

 datetime (2024, 1, 1) : hours (6) : datetime (2024, 1, 1, 18, 0, 0)
ans =
  1x4 datetime array

    01-Jan-2024 00:00:00    01-Jan-2024 06:00:00    01-Jan-2024 12:00:00    01-Jan-2024 18:00:00