Convert Between Datetime Arrays, Numbers, and Text
In this section...
“Overview” on page 7-42
“Convert Between Datetime and Character Vectors” on page 7-42
“Convert Between Datetime and String Arrays” on page 7-44
“Convert Between Datetime and Date Vectors” on page 7-44
“Convert Serial Date Numbers to Datetime” on page 7-45
“Convert Datetime Arrays to Numeric Values” on page 7-45
Overview
datetime is the best data type for representing points in time. datetime values have flexible
display formats and up to nanosecond precision, and can account for time zones, daylight saving
time, and leap seconds. However, if you work with code authored in MATLAB R2014a or earlier, or if
you share code with others who use such a version, you might need to work with dates and time
stored in one of these three formats:
• Date String on page 7-42 — A character vector.
Example: Thursday, August 23, 2012 9:45:44.946 AM
• Date Vector on page 7-44 — A 1-by-6 numeric vector containing the year, month, day, hour,
minute, and second.
Example: [2012
8
23
9
45
44.946]
• Serial Date Number on page 7-45 — A single number equal to the number of days since January
0, 0000 in the proleptic ISO calendar (specifying use of the Gregorian calendar). Serial date
numbers are useful as inputs to some MATLAB functions that do not accept the datetime or
duration data types.
Example: 7.3510e+005
Date strings, vectors, and numbers can be stored as arrays of values. Store multiple date strings in a
cell array of character vectors, multiple date vectors in an m-by-6 matrix, and multiple serial date
numbers in a matrix.
You can convert any of these formats to a datetime array using the datetime function. If your
existing MATLAB code expects a serial date number or date vector, use the datenum or datevec
functions, respectively, to convert a datetime array to the expected data format. To convert a
datetime array to character vectors, use the char or cellstr functions.
Starting in R2016b, you also can convert a datetime array to a string array with the string
function.
Convert Between Datetime and Character Vectors
A date string can be a character vector composed of fields related to a specific date and/or time.
There are several ways to represent dates and times in text format. For example, all of the following
are character vectors representing August 23, 2010 at 04:35:42 PM:
7 Dates and Time
7-42
In this section...
“Overview” on page 7-42
“Convert Between Datetime and Character Vectors” on page 7-42
“Convert Between Datetime and String Arrays” on page 7-44
“Convert Between Datetime and Date Vectors” on page 7-44
“Convert Serial Date Numbers to Datetime” on page 7-45
“Convert Datetime Arrays to Numeric Values” on page 7-45
Overview
datetime is the best data type for representing points in time. datetime values have flexible
display formats and up to nanosecond precision, and can account for time zones, daylight saving
time, and leap seconds. However, if you work with code authored in MATLAB R2014a or earlier, or if
you share code with others who use such a version, you might need to work with dates and time
stored in one of these three formats:
• Date String on page 7-42 — A character vector.
Example: Thursday, August 23, 2012 9:45:44.946 AM
• Date Vector on page 7-44 — A 1-by-6 numeric vector containing the year, month, day, hour,
minute, and second.
Example: [2012
8
23
9
45
44.946]
• Serial Date Number on page 7-45 — A single number equal to the number of days since January
0, 0000 in the proleptic ISO calendar (specifying use of the Gregorian calendar). Serial date
numbers are useful as inputs to some MATLAB functions that do not accept the datetime or
duration data types.
Example: 7.3510e+005
Date strings, vectors, and numbers can be stored as arrays of values. Store multiple date strings in a
cell array of character vectors, multiple date vectors in an m-by-6 matrix, and multiple serial date
numbers in a matrix.
You can convert any of these formats to a datetime array using the datetime function. If your
existing MATLAB code expects a serial date number or date vector, use the datenum or datevec
functions, respectively, to convert a datetime array to the expected data format. To convert a
datetime array to character vectors, use the char or cellstr functions.
Starting in R2016b, you also can convert a datetime array to a string array with the string
function.
Convert Between Datetime and Character Vectors
A date string can be a character vector composed of fields related to a specific date and/or time.
There are several ways to represent dates and times in text format. For example, all of the following
are character vectors representing August 23, 2010 at 04:35:42 PM:
7 Dates and Time
7-42
