Timestamp to Decimal day of year
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Noelle Cielito Soriano
el 1 de Dic. de 2020
Comentada: Noelle Cielito Soriano
el 31 de Dic. de 2020
I have a dataset with a Timestamp column in the format (yyyy-mm-dd hour:minute:second.millisecond) (example: 2003-02-20 01:00:00.100) that I need to convert into decimal day of year, could I please get some guidance on how to do this?
thanks
0 comentarios
Respuesta aceptada
Walter Roberson
el 1 de Dic. de 2020
d = datetime('2003-02-20 01:00:00.100')
floor(days(d - dateshift(d, 'start', 'year'))) + 1
The +1 is because the start of year is day 1 of the year, and the subtraction gives the difference in days -- so Jan 2 would be 1 day difference, Jan 3 would be 2 days difference, and so on. Therefore to get day of year with Jan 1 being day 1, then you need to add 1.
0 comentarios
Más respuestas (1)
Steven Lord
el 1 de Dic. de 2020
dt = datetime('now', 'TimeZone', 'EST')
startOfYear = dateshift(dt, 'start', 'year')
fractionOfYear = years(dt - startOfYear)
11/12
Given that as I type this it's the evening of November 30th (roughly) 11/12th of the year has elapsed.
6 comentarios
Walter Roberson
el 4 de Dic. de 2020
The Answer that I posted computes decimal day number of the year. If the d variable were a vector of values, then the code I posted with floor() would compute the appropriate day number for each element of the array, basing each calculation upon the year appropriate to it. For example, the code would be perfectly happy to be fed dec 2 2020 and jan 2 2021 and would compute the day of the year relative to 2020 for the first and the day of the year relative to 2021 for the second.
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!