Timestamp to Decimal day of year
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Noelle Cielito Soriano
le 1 Déc 2020
Commenté : Noelle Cielito Soriano
le 31 Déc 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 commentaires
Réponse acceptée
Walter Roberson
le 1 Déc 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 commentaires
Plus de réponses (1)
Steven Lord
le 1 Déc 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 commentaires
Walter Roberson
le 4 Déc 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.
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!