Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Checking For Leap Years

This tutorial will show you how to create a JavaScript function that checks whether a given year is a leap year (366 days) or not (365 days).

On Saturday, March 11th 2006 at 10:05 AM
By Lewis Cowles (View Profile)
****-   (Rated 3.9 with 14 votes)
Contextual Ads
More JavaScript Resources
Advertisement

In this tutorial I aim to teach you how to check valid leap years in JavaScript. Basically I want you to be able to understand how to go through the application development cycle to create the most basic of applications so that you have the underpinnings to progress onto more complex projects. I do not state that this code is perfect but it does work. Once you have understood how the code works then you should understand how to improve it and make it run a lot faster.

Step1)Open notepad and enter the following


<html>
<head>
    <script type="text/javascript">


Basically this starts the HTML page and places the script tag inside the header tag so that it can be called from anywhere in this HTML document.



Step2)Now I want you to begin a new function such as the function in the code below


    function isleap(){
        var yr=document.getElementById("year").value;
        if ((parseInt(yr)%4) == 0){
            if (parseInt(yr)%100 == 0){
                if (parseInt(yr)%400 != 0){
                    document.getElementById("leapyear").src = "aintleap.gif";
                    return "false";
                }
                if (parseInt(yr)%400 == 0){
                    document.getElementById("leapyear").src = "isleap.gif";
                    return "true";
                }
            }
            if (parseInt(yr)%100 != 0){
                document.getElementById("leapyear").src = "isleap.gif";
                return "true";
            }
        }
        if ((parseInt(yr)%4) != 0){
            document.getElementById("leapyear").src = "aintleap.gif";
            return "false";
        }    
    }
</script>
</head>


Basically this code is like a procedural handbook that an employer would draft up for all employees. In this handbook it 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. Then The Script tags are ended and the head tag is ended.



Step3) Now that we have a working handbook we need to test the employees skills so that we can be sure the handbook is clear on what is to be done.

 
<body>
Year : <input type="text" id="year" size="4"><br />
<br>
<input type="button" value="Submit" onclick="isleap()">
<br>
<img src="blank.gif" id="leapyear" alt="displays if the year is a leap year">
</body>
</html>

This code defines the store if you will that our little JavaScript employee will be working at so that we can run some tests by your new employee. It defines a form within the body where it gives a said field an ID so that our JavaScript employee knows what to look at. It then tells the html form to call over the assistant once the form is completed.

All done you may have noticed that I got the function to return a value. This is for further expansion where other functions would check if there was a leap year so that for instance you could check the amount of days in February.

I hope this Tutorial has thought you something or you at least thought it was informative to those who may wish to know how to do something like this
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by xmutantduck on Sunday, March 26th 2006 at 04:28 AM

couldn't you just do year mod 4 (parseInt(yr)%4==0), because leap years must be a multiple of 4?

by on Wednesday, April 19th 2006 at 11:12 AM

Unfortunately for xmutantduck, the definition of a leap year is a bit more complicated than most of us learned growing up.

Not all years divisible by 4 are leap years.

A year is a leap year, if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400.

Thus, the year 1900 (which is divisible by both 4 and 100) is NOT a leap year. The year 2000 (which is divisible by 4, 100, and 400) is a leap year.

You can thank Pope Gregory XIII and the non-integral number of days in a solar year for this somewhat complicated scheme.

by Eien Chikara on Tuesday, April 29th 2008 at 11:00 PM

That code seems overcomplicated why don't you just use something like this C code:

if(Year % 400 == 0) leapyear=true;
else if(Year % 100 == 0) leapyear=false;
else if(Year % 4 == 0) leapyear=true;
else leapyear=false;

by undoo on Friday, June 20th 2008 at 10:09 AM

if (((year % 4 == 0)

by pkang on Sunday, September 27th 2009 at 12:07 AM

another way of doing this:

function isLeap(year){
return (year % 400 == 0) || ((year % 4 == 0)

by rainalee on Sunday, September 27th 2009 at 12:09 AM

here is a good article: http://faq.puthik.com/js/js_faq_leapY.html

by raj on Friday, February 5th 2010 at 05:03 AM

hi how r u
u r write

by hi on Friday, February 5th 2010 at 05:06 AM

You can do simply by this function

function isLeapYear(yr) {
return new Date(yr,2-1,29).getDate()==29;
}

by Trent Gardner on Monday, July 26th 2010 at 01:24 AM

function isLeap(year) {
return (year % 400 == 0 || (year % 100 != 0

by Trent Gardner on Monday, July 26th 2010 at 01:26 AM

year % 4 == 0));
}



Your crappy comment engine cut the second line off.

by Trent Gardner on Monday, July 26th 2010 at 01:27 AM

function isLeap(year) {
return (year % 400 == 0 || (year % 100 != 0


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs JavaScript Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources