duration

Length of time as a number of hours, minutes and seconds.

Category: Language Fundamentals > Data Types

Syntax

d = duration(h, m, s)

Builds a duration from hours, minutes and seconds.

d = duration(h, m, s, ms)

Adds milliseconds.

d = duration(___, 'Format', f)

Sets the display format, such as 'hh:mm' or 'd'.

Description

A duration is an exact elapsed time, stored internally in days. Hours do not wrap at 24: duration(25,30,0) is 25:30:00, and duration(0,0,90) is 00:01:30. Durations can be added and subtracted, multiplied by numbers, compared, and added to datetimes. Convert to numbers with hours, minutes, seconds or days.

The functions seconds, minutes, hours, days and years are shorter ways to create durations from a single number.

Input arguments

h, m, s
Numeric arrays of the same size, or scalars. Values outside the usual range carry over.
'Format'
Display format: 'hh:mm:ss' (default), 'hh:mm', 'd', 'hh:mm:ss.SSS', and others.

Output arguments

d
duration array.

Example

Build, combine and convert

d = duration(25, 30, 0)
d + duration(0, 45, 0)
duration(2, 0, 0) > duration(1, 30, 0)
hours(duration(2, 30, 0))
duration(26, 0, 0, 'Format', 'd')
Output
d = duration
   25:30:00
ans = duration
   26:15:00
ans = logical
   1
ans = 2.5000
ans = duration
   1.0833 days

See also

hours minutes seconds days datetime calendarDuration