Arrays That Represent Points in Time - MATLAB
Arrays That Represent Points in Time - MATLAB
datetime
Arrays that represent points in time
Description
datetime arrays represent points in time using the proleptic ISO calendar. datetime values have flexible display
formats up to nanosecond precision and can account for time zones, daylight saving time, and leap seconds.
Creation
Syntax
t = datetime
t = datetime(relativeDay)
t = datetime(DateStrings)
t = datetime(DateStrings,'InputFormat',infmt)
t = datetime(DateVectors)
t = datetime(Y,M,D)
t = datetime(Y,M,D,H,MI,S)
t = datetime(Y,M,D,H,MI,S,MS)
t = datetime(X,'ConvertFrom',dateType)
Description
t = datetime returns a scalar datetime array corresponding to the current date and time.
example
t = datetime(relativeDay) uses the date specified by relativeDay. The relativeDay
input can be 'today', 'tomorrow', 'yesterday', or 'now'.
t = datetime(Y,M,D,H,MI,S) also creates H, MI, and S (hour, minute, and second) arrays. All
arrays must be of the same size (or any can be a scalar). You also can specify the input arguments
as a date vector, [Y M D H MI S].
https://www.mathworks.com/help/matlab/ref/datetime.html 1/9
21/07/2020 Arrays that represent points in time - MATLAB
t = datetime( ___ ,Name,Value) specifies additional options using one or more name-value
pair arguments, in addition to any of the input arguments in the previous syntaxes. For example,
you can specify the display format of t using the 'Format' name-value pair argument.
For best performance when creating datetime values from text, specify either 'Format' or
'InputFormat' and its corresponding value, infmt.
Input Arguments expand all
Day relative to the current date, specified as one of the following values.
Text representing dates and times, specified as a character array, a cell array of character vectors, or a string
array. The datetime function first attempts to match the format of DateStrings to common formats. If you
know the format, specify 'InputFormat' and its corresponding infmt value, or the 'Format' name-value pair
argument.
Example: ["11-Nov-2016","12-Dec-2016"]
Date vectors, specified as an m-by-6 or m-by-3 matrix containing m full or partial date vectors, respectively. A full
date vector has six elements, specifying year, month, day, hour, minute, and second, in that order. A partial date
vector has three elements, specifying year, month, and day, in that order. Each element of DateVector should
be a positive or negative integer value except for the seconds element, which can be fractional. If an element falls
outside the conventional range, datetime adjusts both that date vector element and the previous element. For
https://www.mathworks.com/help/matlab/ref/datetime.html 2/9
21/07/2020 Arrays that represent points in time - MATLAB
example, if the minutes element is 70, then datetime adjusts the hours element by 1 and sets the minutes
element to 10. If the minutes element is -15, then datetime decreases the hours element by 1 and sets the
minutes element to 45.
Year, month, and day arrays specified as numeric arrays. These arrays must be the same size, or any one can be
a scalar. Y,M,D should be integer values.
• If Y,M,D are all scalars or all column vectors, then you can specify the input arguments as a date vector, [Y M
D].
• If an element of the Y, M, or D inputs falls outside the conventional range, then datetime adjusts both that
element and the same element of the previous input. For details, see the description for the DateVectors
input argument.
Example: 2003,10,24
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Y,M,D,H,MI,S,MS — Year, month, day, hour, minute, second, and millisecond arrays
numeric arrays
X — Numeric values
array
Example: 'Format','eeee MMMM d, y','TimeZone','local' applies a display format to datetime values and
specifies the local time zone.
https://www.mathworks.com/help/matlab/ref/datetime.html 3/9
21/07/2020 Arrays that represent points in time - MATLAB
Month number of each value in the datetime array, specified as a numeric array that is the same size and shape
as the datetime array. Each month number is an integer value from 1 to 12. If you set a value outside that
range, then the Year property adjusts accordingly, and the Month property stays within the range 1 to 12. For
example, month 0 corresponds to month 12 of the previous year. For historical dates, the month number is based
on the proleptic Gregorian calendar.
Examples
collapse all
https://www.mathworks.com/help/matlab/ref/datetime.html 4/9
21/07/2020 Arrays that represent points in time - MATLAB
Specify the current date and time in the local system time zone.
View MATLAB Command
Specify the current date and time in the time zone represented by Seoul, Korea
DateStrings = {'2014-05-26';'2014-08-03'};
t = datetime(DateStrings,'InputFormat','yyyy-MM-dd')
t = 2x1 datetime
26-May-2014
03-Aug-2014
The datetime values in t display using the default format, and not the format of the input dates.
Starting in R2016b, you can create string arrays with the string
function and convert them to datetime values. View MATLAB Command
str = string({'2016-03-24','2016-04-19'})
str = 1x2 string
"2016-03-24" "2016-04-19"
Convert the strings, specifying the input format as yyyy-MM-dd. The format must be specified as a character
vector, even though str is a string array.
t = datetime(str,'InputFormat','yyyy-MM-dd')
t = 1x2 datetime
24-Mar-2016 19-Apr-2016
Create a datetime value from text that represents a date and time
to millisecond precision. To convert text in a format that the View MATLAB Command
datetime function cannot parse without more information, specify
the 'InputFormat' name-value pair argument.
d = '2018-06-25 11:23:37.712';
t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS')
t = datetime
25-Jun-2018 11:23:37
The conversion does keep the fractional seconds. However, by default datetime arrays do not display fractional
seconds. To display them, specify either the 'Format' name-value pair or the Format property.
The 'InputFormat' argument applies only to conversions from input text. The Format property specifies the
display for any datetime array.
DateStrings = {'2014-05-26T13:30-05:00';'2014-08-26T13:30-04:00';'2014-09-26T13:30Z'}
DateStrings = 3x1 cell
{'2014-05-26T13:30-05:00'}
{'2014-08-26T13:30-04:00'}
{'2014-09-26T13:30Z' }
Convert the character vectors to datetime values. When specifying the input format, enclose the letter T in single
quotes to indicate that it is a literal character. Specify the time zone of the output datetime array using the
TimeZone name-value pair argument.
t = datetime(DateStrings,'InputFormat','uuuu-MM-dd''T''HH:mmXXX','TimeZone','UTC')
t = 3x1 datetime
26-May-2014 18:30:00
26-Aug-2014 17:30:00
26-Sep-2014 13:30:00
Convert the character vectors in C to datetime values. If your computer is set to a locale that uses English, you
must specify the 'Locale' name-value pair argument to indicate that the strings are in French.
The datetime values in t display in the default format, and in the language MATLAB uses depending on your
system locale.
Y = [2014;2013;2012];
M = 01;
D = [31;30;31];
Create the datetime array.
t = datetime(Y,M,D)
t = 3x1 datetime
31-Jan-2014
30-Jan-2013
31-Jan-2012
Specify a custom display format for the output, using the Format name-value pair argument.
https://www.mathworks.com/help/matlab/ref/datetime.html 7/9
21/07/2020 Arrays that represent points in time - MATLAB
39558 39600
39700 39800
t = datetime(X,'ConvertFrom','excel')
t = 2x2 datetime
20-Apr-2008 01-Jun-2008
09-Sep-2008 18-Dec-2008
Tips
• For a list of datetime functions, see Dates and Time.
• For a list of core MATLAB functions that accept datetime arrays as input arguments, see Core Functions
Supporting Date and Time Arrays.
'InputFormat' month formats M and MM do not recognize names, and MMM does not
recognize abbreviations
Behavior changed in R2020a
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing
Toolbox™.
See Also
NaT | calendarDuration | cellstr | char | duration | leapseconds | string | timezones
Topics
Represent Dates and Times in MATLAB
Set Date and Time Display Format
Specify Time Zones
Generate Sequence of Dates and Time
Introduced in R2014b
https://www.mathworks.com/help/matlab/ref/datetime.html 8/9
21/07/2020 Arrays that represent points in time - MATLAB
https://www.mathworks.com/help/matlab/ref/datetime.html 9/9