MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
C = 1x6 duration array
01:00:00 01:54:00 02:48:00 03:42:00 04:36:00 05:30:00

Sequence of Datetime Values Using Calendar Rules


This example shows how to use the dateshift function to generate sequences of dates
and time where each instance obeys a rule relating to a calendar unit or a unit of time.
For instance, each datetime must occur at the beginning a month, on a particular day of
the week, or at the end of a minute. The resulting datetime values in the sequence are not
necessarily equally spaced.

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
13-Apr-2019 Sat

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 array
15-Apr-2019 Mon 22-Apr-2019 Mon 29-Apr-2019 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

7 Dates and Time

Free download pdf