MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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:

'23-Aug-2010 04:35:06 PM'
'Wednesday, August 23'
'08/23/10 16:35'
'Aug 23 16:35:42.946'

A date string includes characters that separate the fields, such as the hyphen, space, and
colon used here:

d = '23-Aug-2010 16:35:42'

Convert one or more date strings to a datetime array using the datetime function. For
best performance, specify the format of the input date strings as an input to datetime.

NoteThe specifiers that datetime uses to describe date and time formats differ from
the specifiers that the datestr, datevec, and datenum functions accept.

t = datetime(d,'InputFormat','dd-MMM-yyyy HH:mm:ss')

t =

datetime

23-Aug-2010 16:35:42

Although the date string, d, and the datetime scalar, t, look similar, they are not equal.
View the size and data type of each variable.

whos d t

7 Dates and Time

Free download pdf