Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Convert date and time to vector of components

The `datevec`

function creates a
numeric array whose values represent the date and time components
of years, months, days, hours, minutes, and seconds. However, the
best way to represent points in time is by using the `datetime`

data
type. The best way to represent elapsed time is by using the `duration`

or `calendarDuration`

data
types.

`DateVector = datevec(t)`

`DateVector = datevec(DateNumber)`

`DateVector = datevec(DateString)`

`DateVector = datevec(DateString,formatIn)`

`DateVector = datevec(DateString,PivotYear)`

`DateVector = datevec(DateString,formatIn,PivotYear)`

`[Y,M,D,H,MN,S] = datevec(___)`

converts
the datetime values in `DateVector`

= datevec(`t`

)`datetime`

array `t`

to
date vectors. `datevec`

returns an `m`

-by-6
matrix where each row corresponds to a datetime value in `t`

.

converts
one or more date numbers to date vectors. `DateVector`

= datevec(`DateNumber`

)`datevec`

returns
an `m`

-by-6 matrix containing `m`

date
vectors, where `m`

is the total number of date numbers
in `DateNumber`

.

converts text representing dates and times to date vectors. If the
format used in the text is known, specify the format as `DateVector`

= datevec(`DateString`

)`formatIn`

.
Syntaxes without `formatIn`

are significantly slower
than those that include it.

uses `DateVector`

= datevec(`DateString`

,`formatIn`

)`formatIn`

to interpret the dates and times
represented by `DateString`

.

uses `DateVector`

= datevec(`DateString`

,`PivotYear`

)`PivotYear`

to
interpret text that specifies the year as two characters. If the format
used in the text is known, specify the format as `formatIn`

.
Syntaxes without `formatIn`

are significantly slower
than those that include it.

uses `DateVector`

= datevec(`DateString`

,`formatIn`

,`PivotYear`

)`formatIn`

to
interpret the dates and times represented by `DateString`

,
and `PivotYear`

to interpret text that specifies
the year as two characters. You can specify `formatIn`

and `PivotYear`

in
either order.

returns
the components of the date vector as individual variables `[Y,M,D,H,MN,S]`

= datevec(___)`Y`

, `M`

, `D`

, `H`

, `MN`

,
and `S`

(year, month, day, hour, minutes and seconds). `datevec`

returns
milliseconds as a fractional part of the seconds (`S`

)
output.

When computing date vectors,

`datevec`

sets month values less than 1 to 1. Day values,`D`

, less than 1 are set to the last day of the previous month minus |`D`

|. However, if 0 ≤`DateNumber`

< 1, then`datevec(DateNumber)`

returns a date vector of the form`[0 0 0 H MN S]`

, where`H`

,`MN`

, and`S`

are hours, minutes, and seconds, respectively.

The vectorized calling syntax can offer significant performance improvement for large arrays.

Was this topic helpful?