Click here to Skip to main content
11,713,482 members (82,296 online)
Click here to Skip to main content

Is it Leap Year? ....Using Javascript.

, 14 Nov 2007 46.5K 177 4
Rate this:
Please Sign up or sign in to vote.
Is the given year is leap year...? check out by using JavaScript.

Introduction

A leap year is a year with one extra day inserted into February, the leap year is 366 days with 29 days in February as opposed to the normal 28 days.

Leap Year

In the Gregorian calendar, which is the calendar used by most modern countries, the following rules decide which years are leap years:

  1. Every year divisible by 4 is a leap year.
  2. But every year divisible by 100 is NOT a leap year
  3. Unless the year is also divisible by 400, then it is still a leap year.

This means that year 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years, while year 2000 and 2400 are leap years.

This actually means year 2000 is kind of special, as it is the first time the third rule is used in many parts of the world.

In the old Julian Calendar, there was only one rule: Every year divisible by 4 is a leap year. This calendar was used before the Gregorian calendar was adopted.

Here is I'm going to check the given year is leap or not...How to do....check it out..

I don't state that this code is perfect but it does work.

Using the code

Just copy and paste the following function in to script tag inside the header tag so that it can be called from anywhere in this HTML document.

function isleap()
{
 var yr=document.getElementById("year").value;
 if ((parseInt(yr)%4) == 0)
 {
  if (parseInt(yr)%100 == 0)
  {
    if (parseInt(yr)%400 != 0)
    {
    alert("Not Leap");
    return "false";
    }
    if (parseInt(yr)%400 == 0)
    {
    alert("Leap");
    return "true";
    }
  }
  if (parseInt(yr)%100 != 0)
  {
    alert("Leap");
    return "true";
  }
 }
 if ((parseInt(yr)%4) != 0)
 {
    alert("Not Leap");
    return "false";
 } 
}

Basically this code gives the rules to check if a year is a leap year. If the year is no divisible by 4 then it is not a leap year. If the year is divisible by 100 but not by 400 then it is not a leap year. Otherwise the remaining options leave us with a leap year so I hard coded what to do. The Reason I have coded in what to do otherwise is to highlight the two different statements of verifying that something does match and verifying something does not match.

Just call this script function where you want...as follows,

onclick="isleap()"

that's it....

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

S.Vinothkumar
Web Developer
India India
Hi Viewers,

I wish to all. This is Vinoth. This is where I try to condense everything that you need to know about me.

Blog:

visit my blog

Interests:

I'm passionate about a great many things and continually learning about the things that interest me. They are wearable computers, User Interface Design, Artificial life, Industrial music.






You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
koolprasad200320-May-14 23:59
memberkoolprasad200320-May-14 23:59 
GeneralMy vote of 1 Pin
Sravan S13-Dec-12 21:45
memberSravan S13-Dec-12 21:45 
GeneralMy vote of 1 Pin
hpc_beit4-Oct-12 20:24
memberhpc_beit4-Oct-12 20:24 
GeneralMy vote of 1 Pin
VMykyt16-Jul-09 20:43
memberVMykyt16-Jul-09 20:43 
GeneralRe: My vote of 1 Pin
rainaleesorn26-Sep-09 17:59
memberrainaleesorn26-Sep-09 17:59 
GeneralOr just a single line Pin
Jeff Lindholm15-Nov-07 8:20
memberJeff Lindholm15-Nov-07 8:20 
GeneralRe: Or just a single line Pin
Rafa_9-Oct-09 5:18
memberRafa_9-Oct-09 5:18 
GeneralRe: Or just a single line Pin
hpc_beit5-May-12 0:58
memberhpc_beit5-May-12 0:58 
GeneralShorter version [modified] Pin
James Luterek15-Nov-07 3:09
memberJames Luterek15-Nov-07 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 15 Nov 2007
Article Copyright 2007 by S.Vinothkumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid