Search code examples
matlabdatetimedata-conversion

How to convert datetimes to days of the year?


Suppose DT is a vector of type datetime. How can I get their days of the year?

This is an approximation, but I can't find the right function:

month(DT) * 30 + day(DT)

Solution

  • You can use the Matlab's day function with a vector of datetime elements (see the doc)

    t = [datetime('yesterday');datetime('today');datetime('tomorrow')]; % vector of datetime
    
    day( t, 'dayofyear')
    

    that will give the Day-of-year number, from 1 to 365 or 366, depending on the year.

    ans =

    334 335 336