Convert day of year to Date?
163 views (last 30 days)
Show older comments
I want help converting 1 to 1st Jan; 365 to 31st Dec and so on. Basically I want to convert day number of a year into the actual date it corresponds to. Any help will be greatly appreciated. I have done the inverse using the following code
d = datetime('20-Mar-2018')
Dn = day(d,'dayofyear')
0 Comments
Accepted Answer
More Answers (2)
Steven Lord
on 31 Jul 2018
You want to convert the day number to the date in the current year?
% Get today and the start of the current year
d = datetime('today');
s = dateshift(d, 'start', 'year');
% One approach is to add 77-1 to s (1 January, day 1) to get to day 77
day77a = s + 77 - 1
check_a = day(day77a, 'dayofyear')
% Another approach, shift s to the start of day 77-1 after s
day77b = dateshift(s, 'start', 'day', 77-1)
check_b = day(day77b, 'dayofyear')
% A third approach, create "January 77th" of the current year
day77c = datetime(year(d), 1, 77)
check_c = day(day77c, 'dayofyear')
0 Comments
Pratyush Das
on 2 Aug 2018
5 Comments
Steven Lord
on 11 Oct 2022
You could split the function into the integer part 307 and the fractional part 0.234426997 using floor and subtraction.
x = 307.234426997
numFullDays = floor(x)
numPartialDays = x - numFullDays
dt2 = datetime(2019, 1, numFullDays) + days(numPartialDays)
dt2.Format = dt2.Format + ".SSSSSS"
See Also
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!