Convert day of year to Date?

163 views (last 30 days)
Pratyush Das
Pratyush Das on 31 Jul 2018
Commented: Sreeraj T on 14 Oct 2022
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')

Accepted Answer

Pratyush Das
Pratyush Das on 2 Aug 2018
I finally found the answer I was looking for.
N=10; %you can input any other number,The day in question is the 'N'th day of the year
Dt=datetime('1-Jan-2018')+N-1 % This code will give the date of the 'N'th day.

More Answers (2)

Steven Lord
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')

Pratyush Das
Pratyush Das on 2 Aug 2018
I finally found the answer I was looking for. Suppose the Day in question is the 'N'th day of the year. Then the following code will give the exact date corresponding to that day.
Dt=datetime('1-Jan-2018')+N-1
  5 Comments
Steven Lord
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
x = 307.2344
numFullDays = floor(x)
numFullDays = 307
numPartialDays = x - numFullDays
numPartialDays = 0.2344
dt2 = datetime(2019, 1, numFullDays) + days(numPartialDays)
dt2 = datetime
03-Nov-2019 05:37:34
dt2.Format = dt2.Format + ".SSSSSS"
dt2 = datetime
03-Nov-2019 05:37:34.492540

Sign in to comment.

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!