datevec
Convert date and time to vector of components
Syntax
Description
Note
While you can represent dates and times as date vectors, it is
recommended that you use datetime values to
represent points in time, and duration or calendarDuration values
to represent elapsed times. For more information on updating your code,
see Version
History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.
converts the input array to date vectors—that is, numeric
row vectors with six elements that represent the year, month, day, hour, minute,
and second components of the input dates and times.DateVector = datevec(t)
If the input array t is a datetime or
duration array having m elements, then
datevec returns an m-by-6 matrix
where each row corresponds to a value in t.
If
tis adatetimearray, then its values represent points in time. Each row ofDateVectorrepresents a specific date and time.If
tis adurationarray, then its values represent elapsed time. Each row ofDateVectorrepresents a length of time measured in fixed-length time units. For this reason, the values in the second column ofDateVector(representing number of months) are always zeros. For more information, see the Limitations.
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
syntaxes 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 syntaxes 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). The datevec
function returns milliseconds as a fractional part of the seconds
(S) output.
Examples
Input Arguments
Output Arguments
Limitations
If the first input argument is a
datetimearray, a numeric array, or text representing dates and times, thendatevecsets any month values less than 1 to 1 in the output. Day values,D, less than 1 are set to the last day of the previous month minus |D|. However, if 0 ≤DateNumber< 1, thendatevec(DateNumber)returns a date vector of the form[0 0 0 H MN S], whereH,MN, andSare hours, minutes, and seconds, respectively.If the first input argument is a
durationarray, then the output ofdatevecdoes not represent points in time, but rather lengths of time measured in fixed-length time units. Each row of the output matrix represents a duration as the number of years, months, days, hours, minutes, and seconds whose sum is the total length of time represented by the corresponding element of the inputdurationarray.However, a month cannot be a fixed-length time unit because different months have different numbers of days. Therefore the number of months is always zero. Instead,
datevecrepresents a duration as the number of years (each 365.2425 days long), the number of days (each 86,400 seconds long), and the number of hours, minutes, and seconds whose sum is that duration. As another consequence, the number of days can be greater than 31.
Tips
The vectorized calling syntax can offer significant performance improvement for large arrays.