Documentation

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

Create datetime Arrays

datetime Arrays that represent points in time
NaT Not-a-Time

Create duration Arrays

一年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

Create calendarDuration Arrays

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

Extract Components by Time Unit

一年 Year number
quarter Quarter number
month Month number and name
week Week number
day Day number or name
hour Hour number
minute Minute number
second Second number

Split into Numeric Arrays

ymd Year, month, and day numbers of datetime
hms Hour, minute, and second numbers of datetime or duration
split Split calendar duration into numeric and duration units
time Convert time of calendar duration to duration
timeofday Elapsed time since midnight for datetimes
isdatetime Determine if input is datetime array
isduration Determine if input is duration array
iscalendarduration Determine if input is calendar duration array
isnat Determine NaT (Not-a-Time) elements
isdst Determine daylight saving time elements
isweekend Determine weekend elements
timezones List time zones
tzoffset Time zone offset from UTC
between Calendar math differences
caldiff Calendar math successive differences
dateshift Shift date or generate sequence of dates and time
isbetween Determine elements within date and time interval

Convert to Numbers

datenum Convert date and time to serial date number
datevec Convert date and time to vector of components
exceltime Convert MATLAB datetime to Excel date number
juliandate Convert MATLAB datetime to Julian date
posixtime Convert MATLAB datetime to POSIX time
yyyymmdd Convert MATLAB datetime to YYYYMMDD numeric value
addtodate Modify date number by field

Convert to Strings

char Character array
string String array
datestr Convert date and time to string format

Current Date and Time as Numbers or String

now Current date and time as serial date number
clock Current date and time as date vector
date Current date as character vector

Day of Calendar Week or Month

calendar 指定日历蒙特h
eomday Last day of month
weekday Day of week

Elapsed Time as Number

etime Time elapsed between date vectors

Topics

Represent Dates and Times in MATLAB

Usedatetimearrays to store date and time information. These arrays support arithmetic, sorting, comparisons, plotting, and formatted display.

Compare Dates and Time

You can perform an element-by-element comparison of values in twodatetimearrays or twodurationarrays using relational operators, such as>and<.

Set Date and Time Display Format

TheFormatproperty ondatetime,duration, andcalendarDurationarrays controls the display of values in each array. You can change the value of this property. Changing theFormatproperty does not change the values in the array, only their display.

Specify Time Zones

When you create a datetime, it is unzoned by default. If you work with datetime values from multiple time zones or need to account for daylight saving time, you might need to specify a time zone.

跨区域共享代码和数据

Follow these best practices when sharing code that handles dates and time with MATLAB®users in other locales. Write and read dates from other locals.

Core Functions Supporting Date and Time Arrays

Many functions in MATLAB operate on date and time arrays in much the same way that they operate on other arrays.

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 vice versa, and return unexpected output.