0

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

4

2 回答 2

2

How about subtracting the days upto january the 1st of same year?

>> x = clock
>> y = zeros(1,6);
>> y(1) = x(1);
>> y(2:3) = [1 1]
>> mjuliandate(x) - mjuliandate(y)

Edit: And if the hours, min or sec are not being ignored then using mjuliandate is better than juliandate, as it starts counting from midnight rather than noon.

于 2013-11-13T01:58:22.137 回答
1

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.

于 2013-11-13T07:42:10.543 回答