# How can I convert time from seconds to decimal year in Matlab?

16 views (last 30 days)
Yoni Verhaegen -WE-1718- on 25 Oct 2019
Commented: Steven Lord on 25 Oct 2019
I have a dataset which includes the seconds that have passed since 2000-01-01 00:00:00.0 and I would like them to be converted to decimal years (for example 2013.87).
Can anyone help me out on this? Thanks!
An example from the dataset:
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

Guillaume on 25 Oct 2019
First convert your numbers to datetime. Trivially done:
d = [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]; %demo data
dd = datetime(d, 'ConvertFrom', 'epochtime', 'Epoch', '2000-01-01') %optionally specify a Format for display
Personally, I'd leave it like that and use the datetime from here on. It's more likely to be more useful if you want to perform calculations on dates.
y = year(dd) + years(dd - dateshift(dd, 'start', 'year'))
The above extract the year part of the datetime, then adds the duration in years between the datetime and the same datetime shifted to the start of the year.

#### 1 Comment

Steven Lord on 25 Oct 2019
Another way to do this that results in almost the same answer and looks a bit closer to the English description of the task:
d = [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]; %demo data
dd2 = datetime('2000-01-01') + seconds(d);
seconds(dd - dd2) % Very small differences