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

13 views (last 30 days)
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

Answers (1)

Guillaume
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.
If your really want your fractionally year:
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
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

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!