Questions tagged [leap-year]

A leap year is a year containing one additional day, which falls on Feb 29 in the Gregorian calendar.

A leap year (or intercalary or bissextile year) is a year containing one additional day in order to keep the calendar year synchronized with the astronomical or seasonal year.

Because seasons and astronomical events do not repeat in a whole number of days, calendars that have the same number of days in each year, over time, drift with respect to the event that the year is supposed to track. By inserting (or intercalating) an additional day or month into the year, the drift can be corrected. A year that is not a leap year is called a common year.

More about leap years in general are on Wikipedia here.

In computing, we usually are usually referring to the Gregorian Calendar, which inserts an additional day on February 29th into a leap year. A leap year is a year which satisfies both of the following criteria:

  • It is evenly divisible by 4.
  • It is not evenly divisible by 100 unless it is also evenly divisible by 400.

A common implementation of this algorithm is:

bool isLeapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

Leap Year Bugs

Leap years can create bugs in software. This is described in the following post:

What are some examples of leap year bugs?

213 questions
53
votes
26 answers

Leap year calculation

In order to find leap years, why must the year be indivisible by 100 and divisible by 400? I understand why it must be divisible by 4. Please explain the algorithm.
Ram
39
votes
20 answers

Java Code for calculating Leap Year

I am following "The Art and Science of Java" book and it shows how to calculate a leap year. The book uses ACM Java Task Force's library. Here is the code the books uses: import acm.program.*; public class LeapYear extends ConsoleProgram { …
Ibn Saeed
  • 2,921
  • 10
  • 45
  • 58
28
votes
9 answers

Easy way to determine leap year in ruby?

Is there an easy way to determine if a year is a leap year?
MikeJ
  • 14,193
  • 20
  • 67
  • 85
26
votes
12 answers

javascript to find leap year

How can I get the code below to work when I have a month of february? Currently it is getting to the day and then stopping before getting to the if to determine whether it is a leap year. if (month == 2) { if (day == 29) { if (year % 4…
Juan Almonte
  • 345
  • 2
  • 8
  • 14
19
votes
6 answers

What are some examples of leap year bugs?

A leap year bug is a code defect that creates a problematic, unintended outcome when executed within the context of a leap year, typically within the proleptic Gregorian calendar system. The last leap year was 2016. The next leap years are 2020 and…
Matt Johnson-Pint
  • 197,368
  • 66
  • 382
  • 508
19
votes
9 answers

Java calculate days in year, or between two dates

Is there a method in any native Java class to calculate how many days were/will be in a specific year? As in, was it a Leap year (366 days) or a normal year (365 days)? Or do I need to write it myself? I'm calculating the number of days between two…
jurchiks
  • 1,206
  • 3
  • 23
  • 50
18
votes
4 answers

PHP: Adding years to a timestamp

In PHP given a UTC timestamp I would like to add exactly N number of years. This should take into consideration leap years. Thank you.
Onema
  • 6,423
  • 11
  • 61
  • 97
17
votes
2 answers

Leap year check using bitwise operators (amazing speed)

Someone on JSPerf dropped an amazingly fast implementation for checking leap years of the ISO calendar (link: Odd bit manipulations): function isLeapYear(year) { return !(year & 3 || year & 15 && !(year % 25)); } Using Node.js, I quickly checked…
Redger
  • 563
  • 2
  • 11
15
votes
7 answers

How to determine the date one day prior to a given date in Java?

I am assuming Java has some built-in way to do this. Given a date, how can I determine the date one day prior to that date? For example, suppose I am given 3/1/2009. The previous date is 2/28/2009. If I had been given 3/1/2008, the previous date…
William Brendel
  • 30,014
  • 14
  • 69
  • 77
11
votes
8 answers

Javascript: calculate number of days in month for a given year

I have a HTML page with 3 dropdowns for the month, day and year and I was wondering if there was a way to populate the month drop down properly depending on the month and year. I haven't done this before on the client side, but it looks like a lot…
Abe
  • 5,806
  • 11
  • 44
  • 74
11
votes
17 answers

How to find leap year programmatically in C

I wrote a program in C to find whether the entered year is a leap year or not. But unfortunately its not working well. It says a year is leap and the preceding year is not leap. #include #include int yearr(int year); void…
user379888
10
votes
4 answers

Get a vector of all days in a year with R

Is there a simple R idiom for getting a vector of the days in a given year? I can do the following which does ok... except for leap years: dtt <- as.Date( paste( as.character(year), "-1-1", sep="") ) + seq( 0,364 ) I could, obviously, add a line to…
JD Long
  • 55,115
  • 51
  • 188
  • 278
10
votes
2 answers

Using regex to match date format in yyyymmdd

The regex should match valid dates in a string in the format YYYYMMDD. For example, aaa_20150327_bbb should be matched but aaa_20150229_bbb not because 2015 is not a leap year. Only year from 2000 to 2099 need to be considered.
Joe Wang
  • 109
  • 1
  • 1
  • 3
9
votes
1 answer

Calculating years between from a leap year

When calculating years between two dates, where the second date is calculated from the first one (this is a simplified example of what I'm working on), LocalDate and Period seem to calculate a year slightly differently. For example, LocalDate date =…
Evan Knowles
  • 7,097
  • 2
  • 32
  • 67
6
votes
2 answers

Java leap year code problems

import java.util.Scanner; public class Hw2JamesVaughn { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); …
Rick Vaughn
  • 61
  • 1
  • 2
1
2 3
14 15