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 ComponentCreate 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:56Get the year values of each datetime in the array. Use dot notation to access the Year
property of t.t_years = t.Yeart_years = 1×32019 2020 2021The output, t_years, is a numeric array.Get the month values of each datetime in t by accessing the Month property.t_months = t.Montht_months = 1×34 5 6You 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