Convert day of year to Date?
59 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pratyush Das
el 31 de Jul. de 2018
Comentada: Sreeraj T
el 14 de Oct. de 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')
0 comentarios
Respuesta aceptada
Más respuestas (2)
Steven Lord
el 31 de Jul. de 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 comentarios
Pratyush Das
el 2 de Ag. de 2018
5 comentarios
Steven Lord
el 11 de Oct. de 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"
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!