File Exchange

## day of year

version 1.2.0.0 (1.68 KB) by
Date vectors converted to day of the year.

Updated 25 Jun 2010

Takes a date vector and returns the day of year, known incorrectly in the Geophysical community as the Julian calender day, i.e. 12/31/2005 is returned as day 365, day 06/22/2010 is returned as 173, etc... The function is vectorized. This function needs etime.m (R2009a and later).

USAGES
julday = datevec2doy(mydate)

INPUT
mydate: Either a 6xN or Nx6 array of date vectors, as output by
functions like datevec.

OUTPUT
julday: An Nx1 array of julian days.

-----------------------------------------------------------------------
EXAMPLE
%Take the current day and add normally distributed random days to the
%date.

mydate = datevec(now)';
mydate = repmat(mydate,1,12);
day = datevec2doy(mydate);

### Cite As

Joshua Carmichael (2021). day of year (https://www.mathworks.com/matlabcentral/fileexchange/27989-day-of-year), MATLAB Central File Exchange. Retrieved .

Tongyao Pu

Edward

on second thought, datenum makes this sorta trivial.

Edward

Did exactly what was advertised. An inverse function (DOY to dd:mm:yy or whatever) would be nice for symetry.

Oleg Komarov

Another faster and straightforward approach:

function dayOfYear = yearday(InDate)
InDate = datenum(InDate);
prevYear = datenum(year(InDate)-1, 12,31);
dayOfYear = InDate-prevYear;
end

Oleg Komarov

This function could be much more useful if it was more flexible on the input, as suggested by Jan.
Also you can avoid using etime with another approach as well:
A H1 line is extremely important!

% Example input
ddate = '07/12/2003';
datevec2doy(datevec(ddate))
%myFcn
yearday(ddate)

% It can be made as a one liner...
function dayOfYear = yearday(InDate)
nw = weeknum(InDate);
fiDay = weekday(InDate);
inDay = weekday([num2str(year(InDate)) '/01/01']);
% Calculate days using weeks
dayOfYear = 8-inDay + (nw-2)*7 + fiDay;
end

Oleg

Jan

Help section: descriptive, examples, date and author defined, but no H1-line.
Function: inputs checked, works correctly. Useful and usable.

Another approach: Let Matlab's powerful DATENUM check and convert the input to accept a variety of formats, e.g. "13-Jan-2008", {"14-Feb-2008", "15-Mar-2010 18:21:13"}, [2010, 6, 6, 18, 21, 13], [now; now], DATEVEC([now, now]), etc:
inNum = floor(datenum(in)); % check, convert, crop time
inVec = datevec(inNum);
inVec(:, 2:3) = 1; % January 1st
out = 1 + inNum(:) - datenum(inVec);
This performs the same as ETIME, but omits the conversion from days to seconds and back to days.

James Tursa

This submission has *nothing* to do with Julian Days. It is a misuse of the term. To see what Julian Date & Day mean you can read here:

http://en.wikipedia.org/wiki/Julian_day

Your function returns a day-of-year number. You should rename this submission datevec2doy or the like and remove the term Julian completely.

Also, something simple like this will not work:

d = datevec([now;now;now]);
datevec2julian(d)

because your function works with columns of vectors and not rows of vectors. Although your examples show that the input should be transposed, it might be nice if you handled both cases.

##### MATLAB Release Compatibility
Created with R2009a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux