Main Content

Dates and Time

Arrays of date and time values that can be displayed in different formats

The date and time data types datetime, duration, and calendarDuration support efficient computations, comparisons, and formatted display of dates and times. Work with these arrays in the same way that you work with numeric arrays. You can add, subtract, sort, compare, concatenate, and plot date and time values. You also can represent dates and times as numeric arrays or as text. For more information, see Represent Dates and Times in MATLAB or watch Date and Time Arrays.


expand all

Points in Time

datetimeArrays that represent points in time
dateshiftShift date or generate sequence of dates and times
eomdayLast day of month
lweekdateDate of last occurrence of weekday in month
nweekdateDate of specific occurrence of weekday in month


yearsDuration in years
daysDuration in days
hoursDuration in hours
minutesDuration in minutes
secondsDuration in seconds
millisecondsDuration in milliseconds
durationLengths of time in fixed-length units

Calendar Durations

calyearsCalendar duration in years
calquartersCalendar duration in quarters
calmonthsCalendar duration in months
calweeksCalendar duration in weeks
caldaysCalendar duration in days
calendarDurationLengths of time in variable-length calendar units

Calendar of Month

calendarCalendar for specified month

Points in Time in Alternate Formats

datenumConvert date and time to serial date number
nowCurrent date and time as serial date number
clockCurrent date and time as date vector
dateCurrent date as character vector
today(Not recommended; use datetime("today")) Current date
eomdate(Not recommended; use dateshift) Last date of month

Extract Components

yearYear number of input date and time
quarterQuarter number of input date and time
monthMonth number or name of input date and time
weekWeek number of input date and time
dayDay number or name of input date and time
weekdayDay of week
hourHour component of input date and time
minuteMinute component of input date and time
secondSeconds component of input date and time
weeknum(Not recommended; use week) Week in year

Split into Components

ymdYear, month, and day numbers of datetime
hmsHour, minute, and second numbers of datetime or duration
datevecConvert date and time to vector of components
splitSplit calendar duration into numeric and duration units
timeConvert time of calendar duration to duration
timeofdayElapsed time since midnight for datetime arrays

datetime Arrays

betweenCalendar math differences
caldiffCalendar math successive differences
tzoffsetTime zone offset from UTC
dateshiftShift date or generate sequence of dates and times

Fixed-Format Arrays

addtodateModify date number by field
etimeTime elapsed between date vectors
months(Not recommended; use between) Number of whole months between dates
isbetweenDetermine elements within date and time interval
isregularDetermine if input times are regular with respect to time or calendar unit
isnatDetermine NaT (Not-a-Time) elements
isdstDetermine daylight saving time elements
isweekendDetermine weekend elements
isdatetimeDetermine if input is datetime array
isdurationDetermine if input is duration array
iscalendardurationDetermine if input is calendar duration array
stringString array
charCharacter array
datestrConvert date and time to string format


convertToConvert datetime values to numeric representations
exceltimeConvert MATLAB datetime to Excel date number
posixtimeConvert MATLAB datetime to POSIX time
juliandateConvert MATLAB datetime to Julian date
yyyymmddConvert MATLAB datetime to YYYYMMDD numeric value
m2xdate(Not recommended; use exceltime) MATLAB date to Excel serial date number
x2mdate(Not recommended; use datetime) Excel serial date number to MATLAB serial date number or datetime value

Times in MATLAB

timezonesList time zones
leapsecondsList all leap seconds supported by datetime data type
matlab.datetime.compatibility.convertDatenumConvert inputs to datetime values in a backward-compatible way



Carryover in Date Vectors and Strings

If an element falls outside the conventional range, MATLAB adjusts both that date vector element and the previous element.

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 interpret serial date numbers as date vectors—and return unexpected output.