Matlab中的月份
我有一个日期数组['1/7/1993';'4/21/1993';'6/11/1993';'2/7/1994';'5/26/1994';'3/15/1995'],并希望将其转换为格式为'mm/yyyy'的日期数组,其中包括最小和最大日期内的所有月份和年份,即datr=['1/1993';'2/1993';'3/1993';….;'3/1995']Matlab中的月份,matlab,date,Matlab,Date,我有一个日期数组['1/7/1993';'4/21/1993';'6/11/1993';'2/7/1994';'5/26/1994';'3/15/1995'],并希望将其转换为格式为'mm/yyyy'的日期数组,其中包括最小和最大日期内的所有月份和年份,即datr=['1/1993';'2/1993';'3/1993';….;'3/1995'] 我怎么做?还有,我如何在Matlab中计算两个日期之间的月数?首先,我希望我在这里没有假设太多,但是Matlab已经内置了这样一个功能,如果您还没有使用
我怎么做?还有,我如何在Matlab中计算两个日期之间的月数?首先,我希望我在这里没有假设太多,但是Matlab已经内置了这样一个功能,如果您还没有使用,应该开始使用 有三种表示日期的方法(直接从文档中) 有一个名为的函数,如果您可以将日期转换为此日期向量格式,则很容易计算两个日期之间的月数:
date1 = datevec('17-jun-04', 'dd-mmm-yy')
date2 = datevec('24-oct-03', 'dd-mmm-yy')
% This if/else checks which date is later than the other
if(datenum(date1) > datenum(date2))
% num months is the number of months left in date2's year (ex. 2 months)+
% the number of months in date1's year (ex. 6 months) +
% the number of months in the years between (ex. 0 months).
num_months = (12 - date2(2)) + date1(2) + 12*(date1(1)-date2(1)-1)
else
%just flipped date1 and date2 from the above.
num_months = (12 - date1(2)) + date2(2) + 12*(date2(1)-date1(2)-1)
end
上述代码用于检查date1是否大于date2(反之亦然)
此外,如果只想显示月份和年份,可以指定如何使用
首先,我希望我没有在这里假设太多,但是MATLAB已经内置了,如果你还没有使用,你应该开始使用 有三种表示日期的方法(直接从文档中) 有一个名为的函数,如果您可以将日期转换为此日期向量格式,则很容易计算两个日期之间的月数:
date1 = datevec('17-jun-04', 'dd-mmm-yy')
date2 = datevec('24-oct-03', 'dd-mmm-yy')
% This if/else checks which date is later than the other
if(datenum(date1) > datenum(date2))
% num months is the number of months left in date2's year (ex. 2 months)+
% the number of months in date1's year (ex. 6 months) +
% the number of months in the years between (ex. 0 months).
num_months = (12 - date2(2)) + date1(2) + 12*(date1(1)-date2(1)-1)
else
%just flipped date1 and date2 from the above.
num_months = (12 - date1(2)) + date2(2) + 12*(date2(1)-date1(2)-1)
end
上述代码用于检查date1是否大于date2(反之亦然)
此外,如果只想显示月份和年份,可以指定如何使用
>> date1
date1 =
2004 6 17 0 0 0
>> datestr(date1,'mm/yyyy') %Displays just month and year
ans =
06/2004