MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Use Functions to Retrieve Date and Time Component


Use the month function to get the month number for each datetime in t. Using functions
is an alternate way to retrieve specific date or time components of t.


m = month(t)


m = 1×3


4 5 6


Use the month function rather than the Month property to get the full month names of
each datetime in t.


m = month(t,'name')


m = 1x3 cell array
{'April'} {'May'} {'June'}


You can retrieve the year, quarter, week, day, hour, minute, and second components of
each datetime in t using the year, quarter, week, hour, minute, and second
functions, respectively.


Get the week of year numbers for each datetime in t.


w = week(t)


w = 1×3


15 20 25


Get Multiple Date and Time Components


Use the ymd function to get the year, month, and day values of t as three separate
numeric arrays.


[y,m,d] = ymd(t)


y = 1×3


2019 2020 2021


Extract or Assign Date and Time Components of Datetime Array
Free download pdf