datetime

Array of points in time with calendar and clock fields.

Category: Language Fundamentals > Data Types

Syntax

t = datetime(y, m, d)

Dates from year, month, day.

t = datetime(y, m, d, h, mi, s)

Adds hour, minute and second.

t = datetime(str)

Parses text such as '2020-03-04'.

t = datetime(str, 'InputFormat', f)

Parses text with an explicit format.

t = datetime(x, 'ConvertFrom', type)

Converts from 'datenum', 'posixtime', 'excel' and others.

Description

A datetime holds a time stamp and can be added to durations (t + hours(5)), subtracted from another datetime to get a duration, compared with < and ==, and queried with fields such as t.Year, t.Month, t.Day. Out-of-range values roll over, so month 13 means January of the next year.

In a pattern for text, MM means month and mm minutes, the opposite of datestr.

Input arguments

y, m, d, h, mi, s
Numeric arrays of the same size, or scalars.
'InputFormat'
Pattern describing the text, such as 'dd/MM/yyyy'.
'Format'
Display format.

Output arguments

t
datetime array.

Example

Build, shift and query

t = datetime(2020,3,4,5,6,7)
t + hours(20)
t.Month
datetime('2020-03-04')
datetime(2020,1,2,'Format','yyyy/MM/dd')
Output
t = datetime
   04-Mar-2020 05:06:07
ans = datetime
   05-Mar-2020 01:06:07
ans = 3
ans = datetime
   04-Mar-2020
ans = datetime
   2020/01/02

See also

duration datenum datestr dateshift between calendarDuration