Converting dates in time series

2 views (last 30 days)
Chris Martin
Chris Martin on 7 Mar 2022
Commented: Star Strider on 8 Mar 2022
Dear All, I have a time seris that containts the time vector as :
27-Feb-2007 12:00:00 58.199 -136.64 -0.0048352
28-Feb-2007 12:00:00 58.199 -136.64 -0.0048265
01-Mar-2007 12:00:00 58.199 -136.64 -0.0048853
02-Mar-2007 12:00:00 58.199 -136.64 -0.0050604
03-Mar-2007 12:00:00 58.199 -136.64 -0.0051769
04-Mar-2007 12:00:00 58.199 -136.64 -0.0052122
I need to change the time format of the time column (first column) to decimal year format. I need the time format something like 2007.0123. please help me

Answers (3)

Davide Masiello
Davide Masiello on 7 Mar 2022
Edited: Davide Masiello on 7 Mar 2022
Take a look at this
date = '27-Feb-2007 12:00:00';
str = datestr(datenum(date),'yyyy.dd.mm')
str =
'2007.27.02'
And for more info you can take a look at the following webpages

Star Strider
Star Strider on 7 Mar 2022
Edited: Star Strider on 7 Mar 2022
One approach —
M = {'27-Feb-2007 12:00:00' 58.199 -136.64 -0.0048352
'28-Feb-2007 12:00:00' 58.199 -136.64 -0.0048265
'01-Mar-2007 12:00:00' 58.199 -136.64 -0.0048853
'02-Mar-2007 12:00:00' 58.199 -136.64 -0.0050604
'03-Mar-2007 12:00:00' 58.199 -136.64 -0.0051769
'04-Mar-2007 12:00:00' 58.199 -136.64 -0.0052122};
format long
T1 = cell2table(M)
T1 = 6×4 table
M1 M2 M3 M4 ________________________ ______ _______ __________ {'27-Feb-2007 12:00:00'} 58.199 -136.64 -0.0048352 {'28-Feb-2007 12:00:00'} 58.199 -136.64 -0.0048265 {'01-Mar-2007 12:00:00'} 58.199 -136.64 -0.0048853 {'02-Mar-2007 12:00:00'} 58.199 -136.64 -0.0050604 {'03-Mar-2007 12:00:00'} 58.199 -136.64 -0.0051769 {'04-Mar-2007 12:00:00'} 58.199 -136.64 -0.0052122
ddoy = day(datetime(T1.M1),'dayofyear')/day(datetime(year(T1.M1),12,31),'dayofyear');
% T1.M1 = year(datetime(T1.M1)) + ddoy(:,1) % Not Rounded
T1.M1 = year(datetime(T1.M1)) + round(ddoy(:,1), 4) % Round To 4 Decimal Places
T1 = 6×4 table
M1 M2 M3 M4 _________ ______ _______ __________ 2007.1589 58.199 -136.64 -0.0048352 2007.1616 58.199 -136.64 -0.0048265 2007.1644 58.199 -136.64 -0.0048853 2007.1671 58.199 -136.64 -0.0050604 2007.1699 58.199 -136.64 -0.0051769 2007.1726 58.199 -136.64 -0.0052122
I hope that there are easier ways to do this using datetime functions. I looked, however there doesn’t appear to be a format option for this.
EDIT — (7 Mar 2022 at 14:20)
Added round call to ‘ddoy’ display.
.
  2 Comments
Chris Martin
Chris Martin on 8 Mar 2022
does it take account the leap years ?
Star Strider
Star Strider on 8 Mar 2022
The datetime function (and related functions) accounts for leap years, leap seconds, and every other known time variant.

Sign in to comment.


Peter Perkins
Peter Perkins on 7 Mar 2022
Here's my solution:
>> tt = timetable(rand(6,1),rand(6,1),'RowTimes',datetime(2007,2,27:32,12,0,0))
tt =
6×2 timetable
Time Var1 Var2
____________________ _______ _______
27-Feb-2007 12:00:00 0.14929 0.92926
28-Feb-2007 12:00:00 0.25751 0.34998
01-Mar-2007 12:00:00 0.84072 0.1966
02-Mar-2007 12:00:00 0.25428 0.25108
03-Mar-2007 12:00:00 0.81428 0.61604
04-Mar-2007 12:00:00 0.24352 0.47329
>> lengthOfYear = @(t) caldays(between(dateshift(t,'start','year','current'),dateshift(t,'start','year','next'),'days'))
lengthOfYear =
function_handle with value:
@(t)caldays(between(dateshift(t,'start','year','current'),dateshift(t,'start','year','next'),'days'))
>>
>> tt.DecimalYear = year(tt.Time) + day(tt.Time,'dayofyear')./lengthOfYear(tt.Time)
tt =
6×3 timetable
Time Var1 Var2 DecimalYear
____________________ _______ _______ ___________
27-Feb-2007 12:00:00 0.14929 0.92926 2007.2
28-Feb-2007 12:00:00 0.25751 0.34998 2007.2
01-Mar-2007 12:00:00 0.84072 0.1966 2007.2
02-Mar-2007 12:00:00 0.25428 0.25108 2007.2
03-Mar-2007 12:00:00 0.81428 0.61604 2007.2
04-Mar-2007 12:00:00 0.24352 0.47329 2007.2
SS, I think this is just about what you did, except for putting the length of year calculation in a function.
Chris, unless you need to export these values, I'd think twice about creating them. It's kind of a terrible time representation in which you get poor precision, and even whole days are subject to round-off errors. SS, that kind of answers why there is not a simpler way to do this, but maybe this is a more common format that I'm aware of.

Categories

Find more on Dates and Time 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!