MATLAB Answers


Calculate the number of days in a year

Asked by FCC
on 21 Apr 2013

Is it possible to calculate the number of days in a year without having to put the year? If so how? Also i'm doing this on an m-file.



No products are associated with this question.

2 Answers

Answer by Royi Avital
on 21 Apr 2013

I would go something like that:

firstDayInYear = '01/01/2012';
lastDayInYear = '31/12/2012';
firstDayInYearNum = datenum(firstDayInYear, 'dd/mm/yyyy');
lastDayInYearNum = datenum(lastDayInYear, 'dd/mm/yyyy');
numDaysInYear = lastDayInYearNum - firstDayInYearNum + 1;

That's it...


Answer by per isakson
on 21 Apr 2013

Or one of

    y = 2002;
    datenum([y+1,1,1]) - datenum([y,1,1])
    sum( eomday( y, [1:12] ) )


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!