在 MATLAB 如何使用自定义格式计算两个日期之间的时间?
In MATLAB How to calculate a time between two dates with a custom format?
我有两个格式如下的日期,想计算这些时间之间的时间:
t1=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF');
t2=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF');
我的输出应该是这样的格式:
(YY:MM:DD:HH:MM:SS:FFF)
举个例子
(0:0:1:2:3:44:25:330)
You can first convert the t1 and t2 char arrays to numeric format
using datenum() function then you can calculate the difference between
the calculated numeric dates back to the required format using
datestr() function.
下面给出了说明该过程的代码。
% storing the two times
t1=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF')
t2=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF')
% convertine the stored times to numeric format
% using datenum()
numeric_t1 = datenum(t1, 'dd-mm-yyyy HH:MM:SS:FFF');
numeric_t2 = datenum(t2, 'dd-mm-yyyy HH:MM:SS:FFF');
% calculating the time difference
time_diff = datestr((numeric_t2 - numeric_t1), 'yy:mm:dd:HH:MM:SS:FFF')
命令Window输出
我有两个格式如下的日期,想计算这些时间之间的时间:
t1=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF');
t2=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF');
我的输出应该是这样的格式:
(YY:MM:DD:HH:MM:SS:FFF)
举个例子 (0:0:1:2:3:44:25:330)
You can first convert the t1 and t2 char arrays to numeric format using datenum() function then you can calculate the difference between the calculated numeric dates back to the required format using datestr() function.
下面给出了说明该过程的代码。
% storing the two times
t1=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF')
t2=datestr(clock,'dd-mm-yyyy HH:MM:SS:FFF')
% convertine the stored times to numeric format
% using datenum()
numeric_t1 = datenum(t1, 'dd-mm-yyyy HH:MM:SS:FFF');
numeric_t2 = datenum(t2, 'dd-mm-yyyy HH:MM:SS:FFF');
% calculating the time difference
time_diff = datestr((numeric_t2 - numeric_t1), 'yy:mm:dd:HH:MM:SS:FFF')
命令Window输出