首页 > 解决方案 > 计算月中的天数 (MATLAB)

问题描述

我想知道从1982-01-01 到 2015-12-31每月的天数。我尝试了一些来自 Matlab 帮助的代码。直到现在我写了这段代码:

t1 = datetime(1982,01,01); %start date
t2 = datetime(2015,12,31); %end date
T = t1:t2; %creating a range

不知道该怎么做。最后,我想要一个数组(1 * 408)谢谢大家

标签: matlabdate

解决方案


这是一种方法。见ndgriddatenum

years = 1982:2015; % desired range of years
[mm, yy] = ndgrid(1:12, years); % all pairs of month, year
result = datenum(yy(:), mm(:)+1, 1) - datenum(yy(:), mm(:), 1); % adding 1 to the month
% works even for December. 'datenum' gives a result where each unit is one day

推荐阅读