FUNCTION REFERENCE
datetime
Points in time as calendar dates and clock times.
Points in time as calendar dates and clock times.
Syntax
t = datetime('now')Syntax
t = datetime(Y, M, D)Syntax
t = datetime(Y, M, D, H, MI, S)Syntax
t = datetime(Y, M, D, H, MI, S, MS)Syntax
t = datetime(DateVectors)Syntax
t = datetime(text, 'InputFormat', fmt)Syntax
t = datetime(X, 'ConvertFrom', kind)Syntax
t = datetime(..., 'Format', fmt, 'TimeZone', tz, 'PivotYear', p)Syntax
t = datetime.emptyEach element holds a date in the proleptic Gregorian calendar and a time of
day to the nanosecond. 'now', 'today', 'tomorrow' and 'yesterday' read the
system clock (local time, or UTC with 'TimeZone','UTC'). Numeric components
combine by implicit expansion and roll over: month 13 is January of the next
year, day 0 the last day of the month before, negative seconds count back.
S and MS add. NaN or Inf in any component gives NaT. DateVectors has 3 or 6
columns, one date per row. Text without 'InputFormat' may be 'yyyy-MM-dd',
'yyyy-MM-dd HH:mm:ss' (also with a 'T' and fractional seconds), 'dd-MMM-yyyy',
'dd-MMM-yyyy HH:mm:ss' or 'MM/dd/yyyy'; text that matches none of them is an
error; text in the 'Format' given is also read. With 'InputFormat', text
that does not match becomes NaT. 'InputFormat' only reads the text: the
display format stays the default unless 'Format' is given, and
'Format','preserveinput' shows the values in the form they were read with.
'ConvertFrom' kinds: 'datenum', 'posixtime', 'excel', 'excel1904', 'yyyymmdd',
'juliandate'. Format letters: yyyy yy u (year), M MM MMM MMMM (month),
d dd (day), DDD (day of year), eee eeee (day name), HH (24-hour), hh (12-hour),
mm (minute), ss (second), S to SSSSSSSSS (fraction of a second), a (AM/PM);
text in single quotes is copied ('yyyy-MM-dd''T''HH:mm:ss'). Month and day
names are English. Two-digit years 'yy' fall in the 100 years starting at
PivotYear (default: the current year less 50). The display format is
'dd-MMM-yyyy HH:mm:ss', or 'dd-MMM-yyyy' for values made from dates only
(text read with an InputFormat that has no time fields counts as dates).
Arithmetic: datetime - datetime is a duration; a datetime plus or minus a
duration, or a number of exact 24-hour days, is a datetime. Comparisons
with NaT are false (~= is true). t1:t2 steps one day, t1:step:t2 takes a
duration or a number of days, and the end is included only when a step
lands on it exactly; for d = t1:t2 steps through the range. Concatenation
keeps the first element's format and time zone (0x0 arrays such as
datetime.empty are dropped); text elements are read with that format. min,
max, mean and median take a dimension, a vector of dimensions or 'all'.
Results beyond the years -100000000 to 100000000 are an error. Properties:
Year, Month, Day, Hour, Minute, Second (set them and the date rolls over),
Format and TimeZone. double(t) is an error: use datenum, posixtime, juliandate or
exceltime.
Example
t = datetime(2024, 2, 29, 12, 30, 0) % 29-Feb-2024 12:30:00Example
t = datetime('2024-03-01') + 1.5 % 02-Mar-2024 12:00:00Example
d = datetime(2024, 3, 1) - datetime(2024, 2, 1) % 696:00:00Example
t = datetime('05/01/2024', 'InputFormat', 'MM/dd/yyyy', 'Format', 'yyyy-MM-dd') % 2024-05-01Example
t = datetime('2024.05.01', 'InputFormat', 'yyyy.MM.dd') % 01-May-2024Example
t = datetime('2024.05.01', 'InputFormat', 'yyyy.MM.dd', 'Format', 'preserveinput') % 2024.05.01Example
m = max([datetime(2024, 1, 1); datetime(2024, 6, 1)], [], 'all') % 01-Jun-2024Example
t = datetime(738000, 'ConvertFrom', 'datenum') % 28-Jul-2020 00:00:00Example
t = datetime(2024, 1, 31) + (0:2) % three daysExample
t = datetime(2024, 1, 1); t.Month = 14 % 01-Feb-2025Differences from MATLAB: Time zones are only '' (unzoned) and 'UTC';
setting TimeZone relabels the values without shifting them, since there is
no time zone database. There are no locale-dependent formats, leap seconds
or daylight saving changes. Arrays are 2-D only. calendarDuration,
between, dateshift, ismember and linspace on datetimes are not
supported yet. Excel serial 60 (29-Feb-1900, which does not exist) gives
NaT. now, clock and date are unchanged and still return numbers and text.
These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.