生成日期与时间的序列

两个端点间已知步长的日期时间或持续时间值的序列

t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,5,8,0,0);
t = t1:t2
t = 1x5 datetime
01-Nov-2013 08:00:00   02-Nov-2013 08:00:00   03-Nov-2013 08:00:00   04-Nov-2013 08:00:00   05-Nov-2013 08:00:00

t = t1:caldays(2):t2
t = 1x3 datetime
01-Nov-2013 08:00:00   03-Nov-2013 08:00:00   05-Nov-2013 08:00:00

t = t1:hours(18):t2
t = 1x6 datetime
01-Nov-2013 08:00:00   02-Nov-2013 02:00:00   02-Nov-2013 20:00:00   03-Nov-2013 14:00:00   04-Nov-2013 08:00:00   05-Nov-2013 02:00:00

d = 0:seconds(30):minutes(3)
d = 1x7 duration
0 sec    30 sec    60 sec    90 sec   120 sec   150 sec   180 sec

t1.TimeZone = 'America/New_York';
t2.TimeZone = 'America/New_York';

t = t1:t2;
dt = diff(t)
dt = 1x4 duration
24:00:00   25:00:00   24:00:00   24:00:00

t = t1:days(1):t2
t = 1x5 datetime
01-Nov-2013 08:00:00   02-Nov-2013 08:00:00   03-Nov-2013 07:00:00   04-Nov-2013 07:00:00   05-Nov-2013 07:00:00

dt = diff(t)
dt = 1x4 duration
24:00:00   24:00:00   24:00:00   24:00:00

t = t1:1:t2
t = 1x5 datetime
01-Nov-2013 08:00:00   02-Nov-2013 08:00:00   03-Nov-2013 07:00:00   04-Nov-2013 07:00:00   05-Nov-2013 07:00:00

添加持续时间或日历持续时间以创建日期的序列

t1 = datetime(2013,11,1,8,0,0);

t = t1 + hours(0:2)
t = 1x3 datetime
01-Nov-2013 08:00:00   01-Nov-2013 09:00:00   01-Nov-2013 10:00:00

t = t1 + calmonths(1:5)
t = 1x5 datetime
01-Dec-2013 08:00:00   01-Jan-2014 08:00:00   01-Feb-2014 08:00:00   01-Mar-2014 08:00:00   01-Apr-2014 08:00:00

t 中的每个日期时间都出现在每个月的第一天。

dt = caldiff(t)
dt = 1x4 calendarDuration
1mo   1mo   1mo   1mo

dt = caldiff(t,'days')
dt = 1x4 calendarDuration
31d   31d   28d   31d

t = datetime(2014,1,31) + calmonths(0:11)
t = 1x12 datetime
31-Jan-2014   28-Feb-2014   31-Mar-2014   30-Apr-2014   31-May-2014   30-Jun-2014   31-Jul-2014   31-Aug-2014   30-Sep-2014   31-Oct-2014   30-Nov-2014   31-Dec-2014

指定日期或持续时间序列的长度和端点

A = datetime(2014,04,14);
B = datetime(2014,08,04);

linspace 的第三个输入参数指定了在两个端点间要生成的线性间隔点的个数。

C = linspace(A,B,5)
C = 1x5 datetime
14-Apr-2014   12-May-2014   09-Jun-2014   07-Jul-2014   04-Aug-2014

A = duration(1,0,0);
B = duration(5,30,0);
C = linspace(A,B,6)
C = 1x6 duration
01:00:00   01:54:00   02:48:00   03:42:00   04:36:00   05:30:00

使用日历规则的日期时间值的序列

t1 = datetime('today','Format','dd-MMM-yyyy eee')
t1 = datetime
25-Jan-2024 Thu

dateshift 的第一个输入参数始终是您要从其生成序列的 datetime 数组。将 'dayofweek' 指定为第二个输入参数，表示输出序列中的日期时间值必须落在一个星期中的特定某天。您可以通过数值或名称来指定一周中的星期几。例如，可以将星期一指定为 2'Monday'

t = dateshift(t1,'dayofweek',2,1:3)
t = 1x3 datetime
29-Jan-2024 Mon   05-Feb-2024 Mon   12-Feb-2024 Mon

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

t1 = datetime(2014,04,01);
t = dateshift(t1,'end','month',0:2)
t = 1x3 datetime
30-Apr-2014   31-May-2014   30-Jun-2014

dt = caldiff(t,'days')
dt = 1x2 calendarDuration
31d   30d

t1 = datetime('now')
t1 = datetime
25-Jan-2024 22:13:12

t = dateshift(t1,'start','hour',0:4)
t = 1x5 datetime
25-Jan-2024 22:00:00   25-Jan-2024 23:00:00   26-Jan-2024 00:00:00   26-Jan-2024 01:00:00   26-Jan-2024 02:00:00

t = dateshift(t1,'start','hour',-1:1)
t = 1x3 datetime
25-Jan-2024 21:00:00   25-Jan-2024 22:00:00   25-Jan-2024 23:00:00