MATLAB Function Reference Previous page   Next Page

Convert date and time to vector of components


datevec is one of three conversion functions that enable you to express dates and times in any of three formats in MATLAB: a string (or date string), a vector of date and time components (or date vector), or as a numeric offset from a known date in time (or serial date number). Here is an example of a date and time expressed in the three MATLAB formats:

A serial date number represents the whole and fractional number of days from 1-Jan-0000 to a specific date. The year 0000 is merely a reference point and is not intended to be interpreted as a real year in time.

V = datevec(N) converts one or more date numbers N to date vectors V. Input argument N can be a scalar, vector, or multidimensional array of positive date numbers. datevec returns an m-by-6 matrix containing m date vectors, where m is the total number of date numbers in N.

V = datevec(S, F) converts one or more date strings S to date vectors V using format string F to interpret the date strings in S. Input argument S can be a cell array of strings or a character array where each row corresponds to one date string. All of the date strings in S must have the same format which must be composed of date format symbols according to the table "Free-Form Date Format Specifiers" in the datestr help. Formats with 'Q' are not accepted by datevec. datevec returns an m-by-6 matrix of date vectors, where m is the number of date strings in S.

Certain formats may not contain enough information to compute a date vector. In those cases, hours, minutes, and seconds default to 0, days default to 1, months default to January, and years default to the current year. Date strings with two character years are interpreted to be within the 100 years centered around the current year.

V = datevec(S, F, P) converts the date string S to a date vector V using date format F and pivot year P. The pivot year is the starting year of the 100-year range in which a two-character year resides. The default pivot year is the current year minus 50 years.

V = datevec(S, P, F) is the same as the syntax shown above, except the order of the last two arguments are switched.

[Y, M, D, H, MI, S] = datevec(...) takes any of the two syntaxes shown above and returns the components of the date vector as individual variables.

V = datevec(S) converts date string S to date vector V. Input argument S must be in one of the date formats 0, 1, 2, 6, 13, 14, 15, 16, or 23 as defined in the reference page for the datestr function. This calling syntax is provided for backward compatibility, and is significantly slower than the syntax which specifies the format string. If the format is known, the V = datevec(S, F) syntax is recommended.

V = datevec(S, P) converts the date string S using pivot year P. If the format is known, the V = datevec(S, F, P) or V = datevec(S, P, F) syntax should be used.

When creating your own date vector, you need not make the components integers. Any components that lie outside their conventional ranges affect the next higher component (so that, for instance, the anomalous June 31 becomes July 1). A zeroth month, with zero days, is allowed.


Obtain a date vector using a string as input:

Obtain a date vector using a serial date number as input:

Assign elements of the returned date vector:

Use free-form date format '' to indicate how you want a nonstandard date string interpreted:

See Also

datenum, datestr, date, clock, now, datetick

Previous page  datetick dbclear Next page

© 1994-2005 The MathWorks, Inc.