SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Leap Year javascript check

    I am using the following code for a counter and am having trouble getting it to check for leap years in the code. Can someone please look at this and tell me where I am going wrong?

    Code:
    function lbls_init()
    {
    	var sd = new Date(lbls_start);
    	var dn = new Date();
    	var nm = Math.floor((dn.getTime()-sd.getTime())/1000);
    
    	_data['sec'] = nm%60;
    	nm = Math.floor(nm/60);
    
    	_data['min'] = nm%60;
    	nm = Math.floor(nm/60);
    
    	_data['hour'] = nm%24;
    	nm = Math.floor(nm/24);
    
    	if (nm >= 365)
    	{
    		_data['day'] = nm-365*Math.floor(nm/365);
    	}
    	else
    	{
    		_data['day'] = nm;
    	}
    
    	nm = Math.floor(nm/365);
    	_data['year'] = nm;
    	lbls_timer();
    }

  2. #2
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the two digits in the end of a number is divisible by 4, the number is a leap year.
    If the two digits in the end of a number is not divisible by 4, the number is not a leap year.

    2010
    The two digits in the end of a number is 10
    10 is not divisible by 4.
    2010 is not a leap year.

    2012
    The two digits in the end of a number is 12
    12 is divisible by 4.
    2012 is a leap year.


    http://www.w3schools.com/jsref/jsref_obj_string.asp

    substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number of character

    http://www.w3schools.com/jsref/jsref_substr.asp

    Code JavaScript:
    <script type="text/javascript">
     
    var y = prompt("enter a year","");
     
    var s = y.substr(2); // the two digits in the end of a number
     
    // alert(s); 
     
    var n = Number(s);
     
    if( (n &#37; 4) == 0) { alert( y + " is a leap year."); }
     
    else { alert( y + " is not a leap year.");}
     
     
    </script>
    Last edited by ScallioXTX; Aug 8, 2010 at 19:25. Reason: Removed unnecessary comment
    The Time Through Ages. In the Name of Allah, Most Gracious, Most Merciful.
    1. By the Time, 2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so how do I incorporate that into the code above?

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't have to take the last two numbers, you can just divide the whole year by 4 (since 100 is divisible by 4).

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The simplest way to check for leap years is to let JavaScript do it for you.

    Code:
    var y = prompt("enter a year","");
    var leapdate = new Date(y, 2, 0);
    if (leapdate.date == 29) alert(y+' is a leap year');
    else alert(y+' is not a leap year');
    It simply checks if the day before the 1st March of the specified year is the 29th and if so then it must be a leap year. As long as the correct leap year rules are built into JavaScript you don't even need to know those rules in order to use it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Member
    Join Date
    Oct 2008
    Location
    NC
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Per http://timeanddate.com:
    1. Every year that is evenly divisible by four is a leap year;
    2. of those years, if it can be evenly divided by 100, it is NOT a leap year, unless
    3. the year is evenly divisible by 400. Then it is a leap year.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ronx3 View Post
    Per http://timeanddate.com:
    1. Every year that is evenly divisible by four is a leap year;
    2. of those years, if it can be evenly divided by 100, it is NOT a leap year, unless
    3. the year is evenly divisible by 400. Then it is a leap year.
    4. There's an extra leap year needed every 3300 years. Since the current calendar started in 1582 the first of these extras will presumably occur in 4882 and the following one on 8182 but I guess none of us wuill be around to worry about those leap years.

    You don't need to worry about leap year rules though if you let the programming language you are using use the built in code that already knows the part of the rules that is relevant.

    The simplest rule to use with JavaScript is the one that says that if the day before the 1st March is the 29th then it's a leap year. I posted the code that does the test that way earlier in this thread.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Date.isLeapYear= function(y){
    	if(typeof y!= 'number') y= new Date().getFullYear();
    	return new Date(y, 1, 29).getDate()== 29;
    }
    // test
    Date.isLeapYear() // uses current year
    Date.isLeapYear(1900) // returns false
    Date.isLeapYear(2012) // returns true
    Date.isLeapYear(2000) // returns true

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mrhoo View Post
    Date.isLeapYear()
    Excellent, you can't get anything simpler or more flexible than your implementation.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    1. Every year that is evenly divisible by four is a leap year;
    2. of those years, if it can be evenly divided by 100, it is NOT a leap year, unless
    3. the year is evenly divisible by 400. Then it is a leap year.
    Interesting, glad to have learned that. Also I agree with felgall, mrhoo's function is pretty nice.

  11. #11
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While not wanting to bring planes down with my evil bug, just dividing by 4 is safe between 1901 and 2099. Having said that, Mr Hoo's method is so short and neat, you might as well be safe.

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by r51 View Post
    Mr Hoo's method is so short and neat, you might as well be safe.
    Before 4882 JavaScript should have been updated to recognise that year as being a leap year as well and so letting JavaScript work it out will continue to work forever instead of needing updating to take into account one less leap year every 400 years and one more every 3300 years if you didn't include them in your calculation.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •