File Exchange

image thumbnail

day of year

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


Updated 25 Jun 2010

View Version History

View License

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).

julday = datevec2doy(mydate)

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

julday: An Nx1 array of julian days.

%Take the current day and add normally distributed random days to the

tadd = randn(1,12);
mydate = datevec(now)';
mydate = repmat(mydate,1,12);
mydate(2,:) = mydate(2,:) + tadd;
day = datevec2doy(mydate);

Cite As

Joshua Carmichael (2021). day of year (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Tongyao Pu


on second thought, datenum makes this sorta trivial.


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;

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';
% Your fcn

% 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;



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:

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]);

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!