如何在Matlab中将时间从秒转换为十进制年份?

时间:2019-10-25 10:14:56

标签: matlab datetime time

我有一个数据集,其中包含自2000年1月1日00:00:00.0起经过的秒数,我希望将它们转换为十进制年份(例如2013.87)。

数据集中的示例:

416554767.293262
416554768.037637
416554768.782013
416554769.526386
416554770.270761
416554771.015136
416554771.759509
416554772.503884
416554773.248258
416554773.992632
416554774.737007
416554775.481381
416554776.225757
416554776.970131
416554777.714504
416554778.458880

有人可以帮我吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

您应该能够使用datetimeduration的方法执行这些计算。有点像这样。我一直在谨慎对待每年的秒数,因为当然,这取决于所讨论的年份是否是that年。

% Original data
data = [416554767.293262
416554768.037637
416554768.782013
416554769.526386
416554770.270761
416554771.015136
416554771.759509
416554772.503884
416554773.248258
416554773.992632
416554774.737007
416554775.481381
416554776.225757
416554776.970131
416554777.714504
416554778.458880];

% Original data is seconds since 'base':
base = datetime(2000,1,1);
% Get datetimes corresponding to 'data'
dates = base + seconds(data);
% Extract the year portion from the dates
wholeYears = year(dates);
% Extract the remainder of the dates as seconds
remainderInSeconds = seconds(dates - datetime(wholeYears,1,1));
% Calculate the number of seconds in each of the years
secondsPerYear = seconds(datetime(wholeYears + 1, 1, 1) - datetime(wholeYears, 1, 1));
% Final result is whole years + remainder expressed as years
result = wholeYears + (remainderInSeconds ./ secondsPerYear);
fprintf('%.16f\n', result);
相关问题