Search code examples
matlabdatetimejulian-date

Convert date/time to Ordinal number ONLY in MATLAB


I'm using

c = clock;

in MATLAB to get the current date and time. I want to convert the current date so that I can extract the day number in the year as an integer and store it as a single vector value. I.e day 1 up to day 365

I searched for a Julian Day function but the function jd = juliandate() requires at least 3 elements and formats it with the year and time. I can't seem to find a function that does this. How can I convert the date for just the date number as an integer?

i.e Feb 1st = 32 as an integer

Note: I'd still like to store the time from clock in a separate vector as hh:ss


Solution

  • You could use conversion to datenum:

    c = clock();
    tsNow = datenum(c);
    tsStart = datenum([c(1) 1 1 0 0 0]); % timestamp at the beginning of this year
    daysInYear = tsNow - tsStart;
    

    datenums are just what you want, the number of days since a given fixed timestamp (1-Jan-0000). Hence the difference yields the number of days in a year - including leap years etc. Use floor(daysInYear) if you want the number of full days.