t =
datetime
24-Oct-2012 10:45:07
Instead of using datevec to extract components of datetime values, use functions such as year,
month, and day instead:
y = year(t)
y =
2012
Alternatively, access the corresponding property, such as t.Year for year values:
y = t.Year
y =
2012
Convert Serial Date Numbers to Datetime
A serial date number represents a calendar date as the number of days that has passed since a fixed
base date. In MATLAB, serial date number 1 is January 1, 0000.
Serial time can represent fractions of days beginning at midnight; for example, 6 p.m. equals 0.75
serial days. So the character vector '31-Oct-2003, 6:00 PM' in MATLAB is date number
731885.75.
Convert one or more serial date numbers to a datetime array using the datetime function. Specify
the type of date number that is being converted:
t = datetime(731885.75,'ConvertFrom','datenum')
t =
datetime
31-Oct-2003 18:00:00
Convert Datetime Arrays to Numeric Values
Some MATLAB functions accept numeric data types but not datetime values as inputs. To apply these
functions to your date and time data, convert datetime values to meaningful numeric values. Then,
call the function. For example, the log function accepts double inputs, but not datetime inputs.
Suppose that you have a datetime array of dates spanning the course of a research study or
experiment.
t = datetime(2014,6,18) + calmonths(1:4)
t =
1×4 datetime array
Convert Between Datetime Arrays, Numbers, and Text
7-45
datetime
24-Oct-2012 10:45:07
Instead of using datevec to extract components of datetime values, use functions such as year,
month, and day instead:
y = year(t)
y =
2012
Alternatively, access the corresponding property, such as t.Year for year values:
y = t.Year
y =
2012
Convert Serial Date Numbers to Datetime
A serial date number represents a calendar date as the number of days that has passed since a fixed
base date. In MATLAB, serial date number 1 is January 1, 0000.
Serial time can represent fractions of days beginning at midnight; for example, 6 p.m. equals 0.75
serial days. So the character vector '31-Oct-2003, 6:00 PM' in MATLAB is date number
731885.75.
Convert one or more serial date numbers to a datetime array using the datetime function. Specify
the type of date number that is being converted:
t = datetime(731885.75,'ConvertFrom','datenum')
t =
datetime
31-Oct-2003 18:00:00
Convert Datetime Arrays to Numeric Values
Some MATLAB functions accept numeric data types but not datetime values as inputs. To apply these
functions to your date and time data, convert datetime values to meaningful numeric values. Then,
call the function. For example, the log function accepts double inputs, but not datetime inputs.
Suppose that you have a datetime array of dates spanning the course of a research study or
experiment.
t = datetime(2014,6,18) + calmonths(1:4)
t =
1×4 datetime array
Convert Between Datetime Arrays, Numbers, and Text
7-45
