首页 文章

日期时间转换:数字到日期时间

提问于
浏览
1

我的日期和时间间隔为 one hour(3600 seconds) in number format, e.g 0,3600, 7200, 10800, 14400, 18000 etc. 我有开始日期和时间, e.g 0 corresponds to 2005/06/01 01:00 in 'yyyy/mm/dd HH:MM' format.

我正在将这些数据写入Excel文件,所以我正在寻找能够在写入excel文件之前将以小时(以秒为单位)给出的时间转换为 Date Time (2005/06/01 01:00, 2005/06/01 02:00 etc) 的方式 .

我已经探索过'datenum'和'datestr'函数,但它们没有用,因为我无法给它们定制的开始时间,即(0对应于2005/06/01 01:00) . 如果有人可以帮我指出正确的方向,可能就是这样 .

tempMatrix = [NrID time_inSec ff X Y];
tempMatrix_dataCell=num2cell(tempMatrix);
col_header={'NrID','Time','ff','X','Y'};
data_for_xls_file=[col_header; tempMatrix_dataCell];
xlswrite('My_file.xls',data_for_xls_file);

time_inSec是需要转换的值为0,3600,7200,10800等的列 .

当我使用 datenum 时它返回7.3246e 05所以当我添加3600以获得2005/06/01 02:00并将其传递给 datestr 时它返回2015/04/10 01:00 .

temp_time = datenum('2005/06/01 01:00','yyyy/mm/dd HH:MM')

1 回答

  • 0

    这适用于给定的开始时间:

    startTime = datenum('2005/06/01 01:00', 'yyyy/mm/dd HH:MM'); % Define start time.
    currentTime = datenum('2005/06/01 02:00', 'yyyy/mm/dd HH:MM'); % Current time.
    timePassedHours = (currentTime - startTime) * 24; % Time that has passed in hours.
    display(timePassedHours); % Print the output.
    

相关问题