Main Content

Dates and Time

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

The date and time data typesdatetime,duration, andcalendarDurationsupport 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, seeRepresent Dates and Times in MATLABor watchDate and Time Arrays.

Functions

expand all

Points in Time

datetime Arrays that represent points in time
dateshift Shift date or generate sequence of dates and times
NaT Not-a-Time
eomday Last day of month
lweekdate Date of last occurrence of weekday in month
nweekdate Date of specific occurrence of weekday in month

Durations

一年s Duration in years
days Duration in days
hours Duration in hours
minutes Duration in minutes
seconds Duration in seconds
milliseconds Duration in milliseconds
duration Lengths of time in fixed-length units

Calendar Durations

calyears Calendar duration in years
calquarters Calendar duration in quarters
calmonths Calendar duration in months
calweeks Calendar duration in weeks
caldays Calendar duration in days
calendarDuration Lengths of time in variable-length calendar units

Calendar of Month

calendar 指定月日历

Points in Time in Alternate Formats

datenum Convert date and time to serial date number
now Current date and time as serial date number
clock Current date and time as date vector
date Current date as character vector
today (Not recommended; usedatetime("today")) Current date
eomdate (Not recommended; usedateshift) Last date of month

Extract Components

一年 Year number of input date and time
quarter Quarter number of input date and time
month Month number or name of input date and time
week Week number of input date and time
day Day number or name of input date and time
weekday Day of week
hour Hour component of input date and time
minute Minute component of input date and time
second Seconds component of input date and time
weeknum (Not recommended; useweek) Week in year

Split into Components

ymd Year, month, and day numbers of datetime
hms Hour, minute, and second numbers of datetime or duration
datevec Convert date and time to vector of components
split Split calendar duration into numeric and duration units
time Convert time of calendar duration to duration
timeofday Elapsed time since midnight fordatetimearrays

datetimeArrays

between Calendar math differences
caldiff Calendar math successive differences
tzoffset Time zone offset from UTC
dateshift Shift date or generate sequence of dates and times

Fixed-Format Arrays

addtodate Modify date number by field
etime Time elapsed between date vectors
months (Not recommended; usebetween) Number of whole months between dates
isbetween Determine elements within date and time interval
isregular Determine if input times are regular with respect to time or calendar unit
isnat Determine NaT (Not-a-Time) elements
isdst Determine daylight saving time elements
isweekend 确定周末d elements
isdatetime Determine if input is datetime array
isduration Determine if input is duration array
iscalendarduration Determine if input is calendar duration array
string String array
char Character array
datestr Convert date and time to string format

Conversions

convertTo Convertdatetimevalues to numeric representations
exceltime ConvertMATLABdatetime toExceldate number
posixtime ConvertMATLABdatetime toPOSIXtime
juliandate ConvertMATLABdatetime to Julian date
yyyymmdd ConvertMATLABdatetime to YYYYMMDD numeric value
m2xdate (Not recommended; useexceltime)MATLABdate toExcelserial date number
x2mdate (Not recommended; usedatetime)Excelserial date number toMATLABserial date number ordatetimevalue

Times inMATLAB

timezones List time zones
leapseconds List all leap seconds supported bydatetimedata type
matlab.datetime.compatibility.convertDatenum Convert inputs todatetimevalues in a backward-compatible way

Topics

Troubleshooting

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,datestrmight interpret your input date vectors as vectors of serial date numbers—or interpret serial date numbers as date vectors—and return unexpected output.