how to read day of the year as datetime ?
Show older comments
I have vector of dates, it looks like this
2019001
2019002
.
.
.
2019364
2019365
2019 is year and next number is day of the year.
anybody tell me how to use datenum or datetime here !
I have around 3 years of dara.
Accepted Answer
More Answers (2)
madhan ravi
on 25 Jul 2020
v = regexprep(""+vector,'(\d{4})(\d+)','00/$2/$1');
Wanted = datetime(datestr(v))
2 Comments
madhan ravi
on 25 Jul 2020
Days_in_a_year = double(regexprep(v, '(\d+)/(\d+)/(\d+)', '$2'))
madhan ravi
on 25 Jul 2020
For older versions:
v = regexprep(sprintfc('%d',vector),'(\d{4})(\d+)','00/$2/$1'); % sprintfc() undocumented
Wanted = datetime(datestr(v))
Days_in_a_year = str2double(regexprep(v, '(\d+)/(\d+)/(\d+)', '$2'));
Star Strider
on 25 Jul 2020
Another approach:
ydv = [2019001
2019002
2019364
2019365]; % ‘YearDay’ Vector
Years_Days = datetime(num2str(fix(ydv/1000)), 'InputFormat','yyyy') + caldays(rem(ydv,1000)-1)
producing:
Years_Days =
4×1 datetime array
01-Jan-2019
02-Jan-2019
30-Dec-2019
31-Dec-2019
Use the 'Format' name-value pair to get the desired output format. The easiest way to do that (for one example) is simply:
Years_Days.Format = 'yyyy-MM-dd';
to get:
Years_Days =
4×1 datetime array
2019-01-01
2019-01-02
2019-12-30
2019-12-31
.
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!