File Exchange

image thumbnail


version (18.6 KB) by

Calculate leap year



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.

Comments and Ratings (9)

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

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?

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.

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!


Jan (view profile)

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


Jan (view profile)

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


inputparser and github link

Changes in description

Modified after Simon Jan comment (also added into the help lines)

Change in description

Changes in help section

Improvement in input error handling

another logial indexing improvement

more elegant use of logical operators

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

MATLAB Online Live Editor Challenge

Win cash prizes and have your live script featured on our website

Learn more

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video