Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2012, 01:07 AM   PM User | #1
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
Leap Year?

The problem with most leap year evaluation calculations is that they do not fully take in to account that some leap years just do not exist and are skipped.

As you trawl the internet you will find thousands of examples that all boil down to using modulo math to obtain a true or false boolean value.

Here is a simpler and far more accurate way of dealing with the "Leap Year" problem that uses a prototype constructor to extend the Number object.

Code:
// Leap year problem
Number.prototype.isLeapYear = function(){
return (new Date(this,1,29).getMonth()===1);
}
// alert((2008).isLeapYear());
How? Why?

Simply put, the Javascript date object will automatically roll over the days to the following month, 1st March if 29th of Feb does not exist. This means that the test for month 1 (Feb in JavaScript) will return a boolean true if it happens to be Feb 29th.

You do not need to "Calculate" as the calculation is done within the operating system which is where javascript gets all its times and calculations from. So the days of x % 4 and x % 400 are long gone with a faster method of evaluation that is more accurate.
Cremator is offline   Reply With Quote
The Following 3 Users Say Thank You to Cremator For This Useful Post:
Dormilich (01-27-2012), Philip M (01-27-2012), Solushunz (02-08-2012)
Old 01-28-2012, 01:01 AM   PM User | #2
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,427
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
Niiiiiiice.
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is offline   Reply With Quote
Old 01-28-2012, 03:12 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
I would suspect that the code will report the wrong answer for the year 4882 and 8182 which are the only 2 four digit leap years not divisible by 4.

Leap year rules.

1. Divisible by 4 and not divisible by 100.
2. Divisible by 400.
3. Year less 1582 divisible by 3300.

While most code now implements the first two leap year rules most people don't even know about the third one in order to implement it. Not that it is likely that anyone is likely to need to worry about it for a couple of thousand years or so - and so that the code doesn't give correct results for those years is unlikely to matter for most applications.

Similar code could be implemented on the Date object to report if the date stored in it is a leap year. That way you could determine if a date is in a leap year without having to extract the year to test it.

Code:
Date.prototype.isLeapYear = function(){
return (new Date(this.getFullYear(),1,29).getMonth()===1);
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 01-28-2012, 06:46 AM   PM User | #4
Cremator
New Coder

 
Join Date: Jan 2012
Location: in the uk.
Posts: 99
Thanks: 2
Thanked 6 Times in 4 Posts
Cremator has a little shameless behaviour in the past
Quote:
Originally Posted by felgall View Post
I would suspect that the code will report the wrong answer for the year 4882 and 8182 which are the only 2 four digit leap years not divisible by 4.

Leap year rules.

1. Divisible by 4 and not divisible by 100.
2. Divisible by 400.
3. Year less 1582 divisible by 3300.

While most code now implements the first two leap year rules most people don't even know about the third one in order to implement it. Not that it is likely that anyone is likely to need to worry about it for a couple of thousand years or so - and so that the code doesn't give correct results for those years is unlikely to matter for most applications.

Similar code could be implemented on the Date object to report if the date stored in it is a leap year. That way you could determine if a date is in a leap year without having to extract the year to test it.

Code:
Date.prototype.isLeapYear = function(){
return (new Date(this.getFullYear(),1,29).getMonth()===1);
}
I will generally avoid "Fixing" the year and allow the user to pass or use the year to be tested, more friendly and flexible.

I tested the function against published tables, the function works fine, you have to remember that the reference is the tables within javascript. Any limitations are down to the scope of the table of rules which will be far more accurate than using modulo math to derive the answer.

So for the now an present, nothing wrong with the method as I really don't see javascript existing in about 2,000 years or more either.

So in fairness, we are plain sailing for a bout 3,000 years until the hic-up occurs but by then I am sure that any javascript that may still exist will have had the date object "Fixed" by then.

The main problem with any method is that it does have a limitation that means is simply that the length of the vernal equinox year will have changed by an amount that cannot be accurately predicted past a certain point and that is where you pick up on this limitation.

So for the sake of argument, the functions good for a few thousand years and at the whim of Microsoft ensuring that the date / time rules tables are maintained in the operating system.
Cremator is offline   Reply With Quote
Old 01-28-2012, 08:47 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Cremator View Post
So for the now an present, nothing wrong with the method as I really don't see javascript existing in about 2,000 years or more either.
I agree. I am sure that whatever languages are around by the time the extra leap year rules become relevant to most people will be able to handle it.

If you take a look at the version of the method I posted for adding to Date objects that I didn't bother with that extra leap year rule there either.

For the extremely rare situation where that extra rule might be relevant now special code would be needed to handle it anyway.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.