Timestamp to Decimal day of year
9 views (last 30 days)
Show older comments
Noelle Cielito Soriano
on 1 Dec 2020
Commented: Noelle Cielito Soriano
on 31 Dec 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 Comments
Accepted Answer
Walter Roberson
on 1 Dec 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 Comments
More Answers (1)
Steven Lord
on 1 Dec 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 Comments
Walter Roberson
on 4 Dec 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.
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!