datetime

Methods

Method Reference: datetime.minus

datetime: C = minus (A, B)

Subtraction for datetime arrays.

C = minus (A, B) is the equivalent of the syntax C = A - B and supports the following operand combinations.

  • datetime - datetime returns a duration array holding the elapsed time between the corresponding elements. Both operands must either both have a time zone or both be unzoned; a zoned difference is computed from the absolute instants, so the two time zones may differ.
  • datetime - duration returns a datetime array shifted earlier by a fixed number of 24-hour days. For a zoned array the shift is applied to the absolute instant, so it is aware of daylight saving time transitions.
  • datetime - calendarDuration returns a datetime array shifted earlier in calendar units. Whole months (and years) are applied first, clamping the day of month to the last day of the target month when necessary (e.g. 31 March minus one month is 28 February), then whole calendar days, and finally the time-of-day component as an instant.
  • datetime - X, where X is a numeric or logical array, treats the elements of X as a number of fixed 24-hour days.

A and B must be size compatible: they can be the same size, one can be scalar, or for every dimension their sizes must be equal or one of them must be 1. Not-A-Time and infinite elements propagate to the result.

Source Code: datetime

Subtracting two datetimes gives the elapsed time between them as a duration.

 datetime (2024, 3, 15) - datetime (2024, 3, 9)
ans =
  duration

   144:00:00
 datetime (2024, 3, 9, 12, 0, 0) - datetime (2024, 3, 9, 6, 0, 0)
ans =
  duration

   06:00:00

Subtracting a duration shifts by a fixed amount, while subtracting a calendarDuration is calendar-aware — one month before 31 March clamps to the last day of February.

 datetime (2024, 3, 9) - days (3)
ans =
  datetime

   06-Mar-2024
 datetime (2024, 3, 31) - calmonths (1)
ans =
  datetime

   29-Feb-2024