Extract or Assign Date and Time Components of
Datetime Array
This example shows two ways to extract date and time components from existing
datetime arrays: accessing the array properties or calling a function. Then, the example
shows how to modify the date and time components by modifying the array properties.
Access Properties to Retrieve Date and Time Component
Create a datetime array.
t = datetime('now') + calyears(0:2) + calmonths(0:2) + hours(20:20:60)
t = 1x3 datetime array
13-Apr-2019 23:12:56 14-May-2020 19:12:56 15-Jun-2021 15:12:56
Get the year values of each datetime in the array. Use dot notation to access the Year
property of t.
t_years = t.Year
t_years = 1×3
2019 2020 2021
The output, t_years, is a numeric array.
Get the month values of each datetime in t by accessing the Month property.
t_months = t.Month
t_months = 1×3
4 5 6
You can retrieve the day, hour, minute, and second components of each datetime in t by
accessing the Hour, Minute, and Second properties, respectively.
7 Dates and Time