Main Content

Converting Date Vector Returns Unexpected Output

Because a date vector is a 1-by-6 vector of numbers, datestr might interpret your input date vectors as vectors of serial date numbers, or vice versa, and return unexpected output.

Consider a date vector that includes the year 3000. This year is outside the range of years that datestr interprets as elements of date vectors. Therefore, the input is interpreted as a 1-by-6 vector of serial date numbers:

datestr([3000 11 05 10 32 56])

ans =


Here datestr interprets 3000 as a serial date number, and converts it to the date string '18-Mar-0008'. Also, datestr converts the next five elements to date strings.

When converting such a date vector to a character vector, first convert it to a serial date number using datenum. Then, convert the date number to a character vector using datestr:

dn = datenum([3000 11 05 10 32 56]);
ds = datestr(dn)

ds =

05-Nov-3000 10:32:56

When converting dates to character vectors, datestr interprets input as either date vectors or serial date numbers using a heuristic rule. Consider an m-by-6 matrix. datestr interprets the matrix as m date vectors when:

  • The first five columns contain integers.

  • The absolute value of the sum of each row is in the range 1500–2500.

If either condition is false, for any row, then datestr interprets the m-by-6 matrix as m-by-6 serial date numbers.

Usually, dates with years in the range 1700–2300 are interpreted as date vectors. However, datestr might interpret rows with month, day, hour, minute, or second values outside their normal ranges as serial date numbers. For example, datestr correctly interprets the following date vector for the year 2014:

datestr([2014 06 21 10 51 00])

ans =

21-Jun-2014 10:51:00

But given a day value outside the typical range (1–31), datestr returns a date for each element of the vector:

datestr([2014 06 2110 10 51 00])

ans =


When you have a matrix of date vectors that datestr might interpret incorrectly as serial date numbers, first convert the matrix to serial date numbers using datenum. Then, use datestr to convert the date numbers.

When you have a matrix of serial date numbers that datestr might interpret as date vectors, first convert the matrix to a column vector. Then, use datestr to convert the column vector.