Dates on Specific Day of Week
Generate a sequence of dates consisting of the next three occurrences of Monday. First, define
today's date.
t1 = datetime('today','Format','dd-MMM-yyyy eee')
t1 = datetime
01-Sep-2021 Wed
The first input to dateshift is always the datetime array from which you want to generate a
sequence. Specify 'dayofweek' as the second input to indicate that the datetime values in the
output sequence must fall on a specific day of the week. You can specify the day of the week either by
number or by name. For example, you can specify Monday either as 2 or 'Monday'.
t = dateshift(t1,'dayofweek',2,1:3)
t = 1x3 datetime
06-Sep-2021 Mon
13-Sep-2021 Mon
20-Sep-2021 Mon
Dates at Start of Month
Generate a sequence of start-of-month dates beginning with April 1, 2014. Specify 'start' as the
second input to dateshift to indicate that all datetime values in the output sequence should fall at
the start of a particular unit of time. The third input argument defines the unit of time, in this case,
month. The last input to dateshift can be an array of integer values that specifies how t1 should be
shifted. In this case, 0 corresponds to the start of the current month, and 4 corresponds to the start
of the fourth month from t1.
t1 = datetime(2014,04,01);
t = dateshift(t1,'start','month',0:4)
t = 1x5 datetime
01-Apr-2014
01-May-2014
01-Jun-2014
01-Jul-2014
01-Aug-2014
Dates at End of Month
Generate a sequence of end-of-month dates beginning with April 1, 2014.
t1 = datetime(2014,04,01);
t = dateshift(t1,'end','month',0:2)
t = 1x3 datetime
30-Apr-2014
31-May-2014
30-Jun-2014
Determine the number of days between each date.
dt = caldiff(t,'days')
dt = 1x2 calendarDuration
31d
30d
The dates are not equally spaced.
7 Dates and Time
7-18
Précédent

- 272/1450

Suivant