dateshift
Moves datetime values to the start or end of a time unit.
Category: Language Fundamentals > Data Types
Syntax
t2 = dateshift(t, 'start', unit)
Start of the unit that contains each element.
t2 = dateshift(t, 'end', unit)
End of that unit.
Description
dateshift(t,'start','month') gives midnight on the first day of the month. The end of a unit is the last instant before the next one begins, which displays as the last date of the unit. A week starts on Sunday.
This is the standard way to group dates by month or year, for example before a groupsummary.
Input arguments
t- datetime array.
unit'year','month','week'or'day'.
Output arguments
t2- datetime array with the size of
t.
Example
Month and year boundaries
t = datetime(2020,5,17,13,30,0);
dateshift(t, 'start', 'month')
dateshift(t, 'end', 'month')
dateshift(t, 'start', 'year')
Output
ans = datetime
01-May-2020
ans = datetime
31-May-2020
ans = datetime
01-Jan-2020
Notes
The 'dayofweek' form (moving to the next Monday, for example) may return the input date unchanged in MatLivre.