File Exchange

image thumbnail


version (12.8 KB) by Giuseppe Cardillo
Calculate leap year

1 Download

Updated 31 May 2018

From GitHub

View Version History

View license on GitHub

ISLEAP True for leap year.
ISLEAP(Year) returns 1 if Year is a leap year and 0 otherwise.
ISLEAP is only set for gregorian calendar, so Year >= 1583

YEAR - Year of interest (default = current year).
You can input a vector of years.
Logical vector.


Calling on Matlab the function: isleap

Answer is: 0

Calling on Matlab the function: x=isleap([2007 2008])

Answer is:
x = 0 1

Created by Giuseppe Cardillo
Modified after Simon Jan suggestions
To cite this file, this would be an appropriate format:
Cardillo G. (2007) Isleap: a simple routine to test if a year is leap.

Cite As

Giuseppe Cardillo (2021). Isleap (, GitHub. Retrieved .

Comments and Ratings (9)

Michael McCarthy

Giuseppe Cardillo

yes it is possible. I'll change to make it compatible

Michael McCarthy

Hi Giuseppe, do you have the Financial Toolbox installed? I get the error 'Undefined function 'year' for input arguments of type 'double'', if I run year(now). 'now' returns a date number, whereas 'year' (year number) requires a datetime value. 'year' (year of date), from the Financial Toolbox, accepts date numbers. Possibly this is the issue?

Giuseppe Cardillo

No, it is not an error.
help now
now Current date and time as date number.
T = now returns the current date and time as a serial date

FLOOR(now) is the current date and REM(now,1) is the current time.
DATESTR(now) is the current date and time as a character vector.

See also date, datenum, datestr, clock.

Michael McCarthy

I think there's a typo in the March 2018 update. Line 35 should be: year(datetime('now')), rather than year(now). Otherwise, this is very useful. Cheers!


Descriptive help, reliable error checking, new version is really efficient, does exactly what the name says.


CLOCK -> DATE -> DATESTR -> STR2DOUBLE waste a lot of time: str2double(datestr(date,10))
Faster: c = clock; default = {c(1)};

Checking the number of inputs can be done easier and faster without VARARGIN, e.g.:
function x = isleap(Year)
switch nargin
case 1, % Your error checks...
case 0, c = clock; Year = c(1);
otherwise error(...);

The permanent disabling of the warning for logical conversion is not secure. You can omit this by simplifying the engine - instead of:
x=ismember(Year,Year(~C | (~A & B)));
call just:
x = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400));
And suddenly the function is 150 times faster (400 times if called without argument).
Good help, H1-line, date, author and reference included. I'll rate this, if its efficiency is encreased.


Cool, just what I needed. Thanks! :o]

Marc FC

MATLAB Release Compatibility
Created with R2014b
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!